69

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?

Omar
  • 39,496
  • 45
  • 145
  • 213
JQuery Mobile
  • 6,221
  • 24
  • 81
  • 134

8 Answers8

127

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)
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    The assembly date is not working for me. The assembly location is pointing to a file in the GAC which is 3 years old. Would `File.GetLastWriteTime` work better? – Jess Nov 17 '14 at 15:36
  • How to put it in the code. I mean shall I simply paste it in the cshtml file?`

    @ViewContext.Controller.GetType().Assembly.GetName().Version

    `
    – Unbreakable Jan 16 '17 at 20:05
  • 1
    See my comment below to get correct datetime of modification. – lukyer Apr 07 '17 at 10:56
24

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()
frennky
  • 12,581
  • 10
  • 47
  • 63
22

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.

thanassis
  • 691
  • 5
  • 11
5

Working solution for correct date of modification after deploy:

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

Can be used in MVC5 too.

lukyer
  • 7,595
  • 3
  • 37
  • 31
4

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.

chridam
  • 100,957
  • 23
  • 236
  • 235
2

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
-->
Chris Barr
  • 29,851
  • 23
  • 95
  • 135
1

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.

Ed Chapel
  • 6,842
  • 3
  • 30
  • 44
0

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>
JohnH
  • 1,920
  • 4
  • 25
  • 32