Reputation: 6301
I have an ASP.NET MVC 4 application. Currently, I am setting the version of the application in the project properties under the "Application" tab. From here, I click the "Assembly Information..." button. Once there, I have entered "1 0 0 *" in the "Assembly version" field.
My question is, how do I show this value on my web page? Currently, I am trying the following
@System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
Unfortunately, it's always printing "0.0.0.0". Realistically, I'd like to have it print 1.0.0.xyz. I would also like to print the date/time when the last build occurred. However, I have no idea how to do that.
What am I doing wrong?
Upvotes: 69
Views: 35520
Reputation: 2133
For an ASP.NET Master Page display w/ VB.NET ...
Declaration in Master.aspx.vb
Public myAssembly As System.Reflection.Assemply
Define assembly in the Page_Load Sub of Master.aspx.vb
myAssembly = System.Relection.Assembly.GetExecutingAssembly
Display on the .Master page
<p>Version: <%Response.Write(myAssembly.GetName().Version.ToString())%></p>
Upvotes: 0
Reputation: 34012
If you need this in a Razor view, this works for me. It needed the System.IO.*
prefix to work for me
<!--
Version @System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(YourNamespace.Program).Assembly.Location).ProductVersion
Last deployed on @System.IO.File.GetCreationTime(typeof(YourNamespace.Program).Assembly.Location)
-->
Outputs the following:
<!--
Version 1.0.0
Last deployed on 04/19/2019 1:36:24 PM
-->
Upvotes: 2
Reputation: 7983
Working solution for correct date of modification after deploy:
@File.GetLastWriteTime(@System.Reflection.Assembly.GetExecutingAssembly().Location)
Can be used in MVC5 too.
Upvotes: 5
Reputation: 103365
This prints the current version number as outlined in your AssemblyInfo.cs
file for printing in an ASP.NET MVC view:
@(typeof(MyController).Assembly.GetName().Version.ToString())
Replacing MyController
of course with your appropriate MVC controller name.
Upvotes: 4
Reputation: 691
In case you are publishing your application on a production server, I would recommend using something like
@String.Format(
"{0:dddd, MMMM d, yyyy HH:mm:ss}",
File.GetLastWriteTime(ViewContext.Controller.GetType().Assembly.Location))
for retrieving the date.
This will print the actual publish date since File.GetCreationTime()
will give you the date the actual assembly dll was first copied on the server.
Upvotes: 22
Reputation: 13934
I usually make HtmlHelper
extension for this purpose. Something like this:
public static class HtmlHelperExtensions
{
public static IHtmlString AssemblyVersion(this HtmlHelper helper)
{
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return MvcHtmlString.Create(version);
}
}
And than inside view you just call:
@Html.AssemblyVersion()
Upvotes: 24
Reputation: 1038780
To print the version number of the assembly in which was defined the controller that rendered this view:
@ViewContext.Controller.GetType().Assembly.GetName().Version
and for the assembly date:
@File.GetCreationTime(ViewContext.Controller.GetType().Assembly.Location)
Upvotes: 127
Reputation: 6932
Your assembly version may be set using the AssemblyFileVersionAttribute
, which must be accessed specifically.
AssemblyFileVersionAttribute attr = typeof(MyController).Assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).OfType<AssemblyFileVersionAttribute>().FirstOrDefault();
if (attr != null)
{
return attr.Version;
}
The MvcDiagnostics Nuget package makes this simple.
Upvotes: 2