Problem getting the AssemblyVersion into a web page using Razor /MVC3

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

    }

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

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

cshtml/vbhtml is dynamic compile to assembly.

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

how about this?


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)