简体   繁体   中英

How to get the product version from a Razor View

I am trying to display my product version in a Razor view ( _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:

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?

Edited for better answer

I am guessing that it is trying to get the version of the Razor Engine, not your application. So a workaround is to get this info in the controller and send it to the view through a 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). It compiles every page/view into separated 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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