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条回答
不美不萌又怎样
2楼-- · 2019-01-30 10:07

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
查看更多
beautiful°
3楼-- · 2019-01-30 10:12

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)

查看更多
狗以群分
4楼-- · 2019-01-30 10:17

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
查看更多
孤傲高冷的网名
5楼-- · 2019-01-30 10:18

cshtml/vbhtml is dynamic compile to assembly.

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

how about this?

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-30 10:18

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>
查看更多
狗以群分
7楼-- · 2019-01-30 10:24

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("");
        }

    }
查看更多
登录 后发表回答