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!
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
HttpApplicationclass.From within an html helper you can get to this:
Note
You might wonder why the code uses the
BaseTypeof 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).