Problem getting the AssemblyVersion into a web page using Razor /MVC3
Solution 1
cshtml/vbhtml is dynamic compile to assembly.
@typeof(YourApplicationNamespace.MvcApplication).Assembly.GetName().Version
how about this?
Solution 2
This works for me. Without needing to explicitly mention the type.
@ViewContext.Controller.GetType().Assembly.GetName().Version
Solution 3
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("");
}
}
Solution 4
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)
Solution 5
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
Related videos on Youtube
Jay Stevens
The first computers I programmed on were an IBM 8088 and then a Color Apple 2. I graduated in 1986 to a Mac-Plus so that I could record digital sequenced music. (What's the most powerful computer in the world? The one people use.) ... then I upgraded to dual floppy drives (Chuck Yeager Flight Simulator required it), then upgraded to a 40MB HD (all my MIDI programming demanded it), but then I was seduced by the PC-Master Race. (What's the most powerful computer in the world? The one that has software to run on it.) In 1990, I discovered SAS, SPSS and Statistics. So while I am .NET/Typescript/HTML/PWA geek architect; I am also a SAS-Ninja-Shadow-Warrior, machine learning, analytics, jedi master and data wrangler as well.
Updated on August 14, 2020Comments
-
Jay Stevens over 3 years
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??
-
Jay Stevens almost 13 yearsGet Calling assembly just goes one level up in the tree... still no good.
-
edhubbell over 11 yearsAnd
@GetType(YourApplicationNamespace.MvcApplication).Assembly.GetName.Version
for all the VB.NETers. Both of us. -
Shawn J. Molloy over 9 yearsI must also share my praise - this tops of my "show the revision number in the app for qa" story along with [assembly: AssemblyVersion("1.0.*")] I get a nice looking build number. Works great.
-
Cristian Diaconescu over 8 yearsFor WebAPI sites:
@typeof(YourDefaultNamespace.WebApiApplication).Assembly.GetName().Version
. It may even work without the default namespace:@typeof(WebApiApplication).Assembly.GetName().Version
-
Mafii about 8 years
@typeof(YourApplicationNamespace.MvcApplication).Assembly.GetName().Version.ToString(3)
returns 3 of the 4 parts of the assembly version if anyone is curious. You can vary between 0 and 4. -
Paul almost 7 yearsI love SO just for these types of answers. It might have been answered way back in 2011 but in 2017 it still proves to be a lifesaver!
-
Jim over 6 yearsThis is actually a better answer than the accepted one IMO, since it makes it clear that you're looking for a Type that's part of your application's assembly. Sort of explains the cause of the problem better.
-
Leonard AB almost 5 yearsin dotnet core 2.1 this works for me:
@{Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;}
then<h1>Ver. @version</h1>
-
Tobias about 2 yearsWorks also on quite old .aspx pages
-
Vedran Mandić almost 2 yearsDope, works on .NET6 also, used it like:
<p class="w-100">Build date:<br />@(new System.IO.FileInfo(ViewContext.GetType().Assembly.Location).LastWriteTime)</p>