Print Version Number in ASP.NET MVC 4 app

前端 未结 7 1974
灰色年华
灰色年华 2020-12-23 19:15

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 \

相关标签:
7条回答
  • 2020-12-23 19:17

    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.

    0 讨论(0)
  • 2020-12-23 19:18

    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.

    0 讨论(0)
  • 2020-12-23 19:21

    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.

    0 讨论(0)
  • 2020-12-23 19:24

    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)
    
    0 讨论(0)
  • 2020-12-23 19:25

    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()
    
    0 讨论(0)
  • 2020-12-23 19:29

    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
    -->
    
    0 讨论(0)
提交回复
热议问题