11

I have an HtmlHelper extension method in an assembly separate from my MVC application assembly. Within the extension method I would like to get the version number of the MVC application assembly. Is this possible?

The calling assembly is the razor view dynamic assembly so that doesn't help. Is there some object nested within the HtmlHelper that can provide me with the version number of the MVC application assembly? I've been exploring the HtmlHelper class documentation but so far haven't found a solution to my problem.

Thanks!

Jeff Camera
  • 5,324
  • 5
  • 44
  • 59

3 Answers3

17

This is a notoriously evil thing - because unfortunately there's no one specific reliable way to do it.

Since it's an MVC application, however, the chances are that it has a Global.asax.cs - therefore it has a locally defined HttpApplication class.

From within an html helper you can get to this:

public static string AppVersion(this HtmlHelper html)
{
  var appInstance = html.ViewContext.HttpContext.ApplicationInstance;
  //given that you should then be able to do 
  var assemblyVersion = appInstance.GetType().BaseType.Assembly.GetName().Version;
  //note the use of the BaseType - see note below
  return assemblyVersion.ToString();
}

Note

You might wonder why the code uses the BaseType of the application instance, and not simply the type. That's because the Global.asax.cs file is the primary type of the MVC application, but then Asp.Net dynamically compiles another HttpApplication type that inherits from that via the Global.asax.

As I said earlier; this works in most MVC sites because they should all have an application class defined in a Global.asax.cs file by convention (because that's the way the project is set up).

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • @KirkWoll That is correct, I want my mvc application assembly version. – Jeff Camera Feb 28 '12 at 17:12
  • Thanks! Your note about the BaseType was correct. That's what was tripping me up. – Jeff Camera Feb 28 '12 at 17:26
  • Also curious... why did you say it is "notoriously evil"? Your solution seems pretty straight forward. Are there any instances where this would fail? – Jeff Camera Feb 28 '12 at 17:44
  • @JohntheRevelator - if you were looking at doing this across all asp.net sites (i.e. not necessarily via HtmlHelper), then its much trickier because many do not have a fixed application class like this. With an MVC site, there might be an issue if the site only uses a global.asax, in which case there will be no application class in the main dll. But as I say, I'm sure most sites will have one, because its the default way of doing things – Andras Zoltan Feb 28 '12 at 20:53
5

Just in case anyone comes across this, here is what worked for me (MVC5 VS2013). Enter straight into the view:

@ViewContext.HttpContext.ApplicationInstance.GetType().BaseType.Assembly.GetName().Version.ToString();
Jordan
  • 2,992
  • 2
  • 20
  • 29
1

Just search for the assembly which should be the source for your version number

AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name.Equals("MyDll")).First().GetName().Version.ToString();
Daniel
  • 10,864
  • 22
  • 84
  • 115
devBGH
  • 19
  • 2