Problem getting the AssemblyVersion into a web pag

2019-01-30 09:39发布

I'm using the following code in a footer in my _Layout.cshtml file to put the AssemblyInfo version data into the footer of every page in my MVC3 site. However:

@System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()

Just prints in the footer:

Revision 0.0.0.0

When I modified the view to display all of the assembly info for the "Executing Assembly" using the following

@System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()

Which prints the following:

Revision App_Web__layout.cshtml.639c3968.hlogy75x, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

This shows that the "Executing Assembly" isn't my main app, it's the view itself.

How do I get the assembly information for the ACTUAL app, not just the individual views??

9条回答
beautiful°
2楼-- · 2019-01-30 10:26

This works for me. Without needing to explicitly mention the type.

@ViewContext.Controller.GetType().Assembly.GetName().Version
查看更多
看我几分像从前
3楼-- · 2019-01-30 10:26

You can get it using Name property as below:

  @System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

is that what you are looking for?

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-30 10:29

You could try to use the GetCallingAssembly(). I'm not sure if that is high enough up the call stack or not, but since Razor actually creates an assembly for each view, it stands to reason that your app would be the calling assembly for the view assembly.

查看更多
登录 后发表回答