使用 Razor/MVC3将 AssemblyVersion 放入网页时遇到的问题

我在 _ Layout.cshtml 文件的页脚中使用以下代码将 AssemblyInfo 版本数据放到 MVC3站点中每个页面的页脚中。然而:

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

只有脚印:

Revision 0.0.0.0

当我修改视图以显示“执行程序集”的所有程序集信息时,使用以下命令

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

打印出以下内容:

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

这表明“执行程序集”不是我的主应用程序,而是视图本身。

如何获得实际应用程序的汇编信息,而不仅仅是单个视图?

45265 次浏览

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.

Using this helper works for me:

    public static HtmlString ApplicationVersion(this HtmlHelper helper)
{
var asm = System.Reflection.Assembly.GetExecutingAssembly();
var version = asm.GetName().Version;
var product = asm.GetCustomAttributes(typeof(System.Reflection.AssemblyProductAttribute), true).FirstOrDefault() as System.Reflection.AssemblyProductAttribute;


if (version != null && product != null)
{
return new HtmlString(string.Format("<span>{0} v{1}.{2}.{3} ({4})</span>", product.Product, version.Major, version.Minor, version.Build, version.Revision));
}
else
{
return new HtmlString("");
}


}

You can get it using Name property as below:

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

is that what you are looking for?

You need to get the assembly of a type in the project:

typeof(MyType).Assembly.Whatever

Where MyType is any type in the MVC project itself (eg, a controller or model, or the MvcApplication class)

cshtml/vbhtml is dynamic compile to assembly.

@typeof(YourApplicationNamespace.MvcApplication).Assembly.GetName().Version

how about this?

Expanding on takepara's answer, if you want a one liner to get the AssemblyInformationalVersionAttribute from a MVC Razor View:

@System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(Zeroarc.Candid.Web.MvcApplication).Assembly.Location).ProductVersion

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

@ViewContext.Controller.GetType().Assembly.GetName().Version

My problem was that I had renamed the namespace afterwards and I got the error above. The problem was the old namespace reference in the Views\Web.config . I had to change it from Project.WebAPI17 to Company.Project.WebAPI17

  <system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Company.Project.WebAPI17" />
</namespaces>
</pages>
</system.web.webPages.razor>

GO to Home Controller and just copy this code :

Rename ActionResult to String

public string Index()


return typeof(Controller).Assembly.GetName().Version.ToString() ;


run view

for an api controller I used this based of other answers

Version = GetType().Assembly.GetName().Version.ToString()