简体   繁体   English

如何从Razor View获取产品版本

[英]How to get the product version from a Razor View

I am trying to display my product version in a Razor view ( _Layout.cshtml ). 我试图在Razor视图中显示我的产品版本( _Layout.cshtml )。 I´m doing something like this: 我做的是这样的:

<script>
alert('@FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion');
alert('@Assembly.GetExecutingAssembly().Location');
</script>

The problem is that the first alert showed me 0.0.0.0 then, I introduced the second alert and it shows me the following location: 问题是第一个警报显示0.0.0.0然后,我介绍了第二个警报,它显示了以下位置:

C:WindowsMicrosoft.NETFramework644.0.30319Temporary ASP.NET Filesoot#35f35b93778aeaApp_Web_ztow0zpu.dll C:WindowsMicrosoft.NETFramework644.0.30319Temporary ASP.NET Filesoot#35f35b93778aeaApp_Web_ztow0zpu.dll

Obviously this is not my assembly file. 显然这不是我的汇编文件。 Is there any easy and clean way to get the assembly version from a Razor view? 是否有任何简单而干净的方法从Razor视图中获取装配版本?

Edited for better answer 编辑为更好的答案

I am guessing that it is trying to get the version of the Razor Engine, not your application. 我猜它是试图获得Razor引擎的版本,而不是你的应用程序。 So a workaround is to get this info in the controller and send it to the view through a viewbag. 因此,解决方法是在控制器中获取此信息并通过viewbag将其发送到视图。

In your controller add - 在您的控制器添加 -

ViewBag.Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

In your view add - 在您的视图中添加 -

<h1>@ViewBag.Version</h1>

The answer is hidden in fact how ASP.NET handles each Page/View (Razor view). 事实上,ASP.NET是如何处理每个页面/视图(Razor视图)的答案。 It compiles every page/view into separated dll. 它将每个页面/视图编译成分离的dll。 Its name could be as your question says: 它的名字可以像你的问题所说:

App_Web_j2tdatrx.dll

And it is really placed inside the (please take a look) 它真的放在里面(请看看)

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\

So if you want to get the Version of "the web" assembly, you have to find it different way. 因此,如果您想获得“Web”程序集的版本,您必须以不同的方式找到它。 Because it is not the executing one. 因为它不是执行者。 To get the Version in this case, do more reflection and find the library which you want to show: eg Firm.Product.Web.dll 要获得此版本的版本,请执行更多反射并找到要显示的库:例如Firm.Product.Web.dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM