Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssemblyInfo versioning ignored in ASP.NET MVC Web App?

Strange one here. My MVC Web Application's version number is not printing correctly to my view according to what is set in AssemblyInfo.cs. The definition I have set in set AssemblyInfo.cs is '1.0.232.0'.

I have tried multiple methods in order to print it:

<%= System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()%>

(results 0.0.0.0)

<%= System.Reflection.Assembly.GetCallingAssembly().GetName().Version.ToString()%>

(results in 2.0.0.0, which is set nowhere in my project.)

<%= typeof(HomeController).GetType().Assembly.GetName().Version.ToString()%>

(results in 2.0.0.0)

This leads me to believe that it simply must not be picking up my AssemblyInfo.cs file? This is also the case if I attempt to use the "Publish" button to publish to IIS on our development server.

Any ideas? Perhaps I'm using the wrong statement to fetch the version number? :\

Thanks guys.

like image 640
GONeale Avatar asked Mar 23 '09 05:03

GONeale


1 Answers

The view is (by default) still largely compiled on-demand, so you con't reliably use GetExecutingAssembly() within the view - however, for me the controller usage works fine:

[assembly: AssemblyVersion("1.2.3.4")]

with:

<h2><%=typeof(MvcApplication4.Controllers.HomeController).Assembly
   .GetName().Version.ToString() %></h2>

shows

1.2.3.4

in the page.

edit The mistake you made was calling typeof(...).GetType() - that is going to give you Type (or a subclass) - so yes, it will be 2.x. /edit

For the extra step of pre-compiling the views, see "MSBuild Task for Compiling Views" here.

Arguably, your view shouldn't be fetching this data itself anyway - it should be put into the ViewData (or similar), perhaps by a base-controller or action-filter.


Re the question about master pages; first, pick a key ;-p

<%=ViewData["AppVersion"] %>

then two options leap to mind: override OnActionExecuting in the controller (or a common base-controller):

    protected override void OnActionExecuting(
        ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["AppVersion"] =
            GetType().Assembly.GetName()
            .Version.ToString(); // probably cached
        base.OnActionExecuting(filterContext);
    }

or create an action-filter:

public class AppVersionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(
         ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["AppVersion"] =
            GetType().Assembly.GetName()
            .Version.ToString(); // probably cached
        base.OnActionExecuting(filterContext);
    }
}

And mark your controllers (classes) or actions (methods) with this attribute:

[HandleError, AppVersion]
public class HomeController : Controller
{ ... }
like image 54
Marc Gravell Avatar answered Nov 13 '22 19:11

Marc Gravell