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 \
In case you are publishing your application on a production server, I would recommend using something like
@String.Format(
"{0:ffffdd, 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.
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.
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.
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)
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()
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
-->