Print Version Number in ASP.NET MVC 4 app

C#asp.net Mvc

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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)

Solution 2 - C#

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()

Solution 3 - C#

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.

Solution 4 - C#

Working solution for correct date of modification after deploy:

@File.GetLastWriteTime(@System.Reflection.Assembly.GetExecutingAssembly().Location)

Can be used in MVC5 too.

Solution 5 - C#

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.

Solution 6 - C#

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
-->

Solution 7 - C#

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.

Solution 8 - C#

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJQuery MobileView Question on Stackoverflow
Solution 1 - C#Darin DimitrovView Answer on Stackoverflow
Solution 2 - C#frennkyView Answer on Stackoverflow
Solution 3 - C#thanassisView Answer on Stackoverflow
Solution 4 - C#lukyerView Answer on Stackoverflow
Solution 5 - C#chridamView Answer on Stackoverflow
Solution 6 - C#FiniteLooperView Answer on Stackoverflow
Solution 7 - C#Ed ChapelView Answer on Stackoverflow
Solution 8 - C#JohnHView Answer on Stackoverflow