Cannot get assembly version for footer

前端 未结 4 1454
北荒
北荒 2021-02-02 08:43

I\'m using the automatic build versioning mentioned in this question (not the selected answer but the answer that uses the [assembly: AssemblyVersion(\"1.0.*\")] te

相关标签:
4条回答
  • 2021-02-02 09:08

    Just as another solution that people may be interested in, I've concocted these helpers to help with this problem:

    public static class HtmlHelperExtensions
    {
        private static string _CachedCurrentVersionDate;
    
        /// <summary>
        /// Return the Current Version from the AssemblyInfo.cs file.
        /// </summary>
        public static string CurrentVersion(this HtmlHelper helper)
        {
            try
            {
                var version = Assembly.GetExecutingAssembly().GetName().Version;
                return version.ToString();
            }
            catch
            {
                return "?.?.?.?";
            }
        }
    
        public static string CurrentVersionDate(this HtmlHelper helper)
        {
            try
            {
                if (_CachedCurrentVersionDate == null)
                {
                    // Ignores concurrency issues - assuming not locking this is faster than 
                    // locking it, and we don't care if it's set twice to the same value.
                    var version = Assembly.GetExecutingAssembly().GetName().Version;
                    var ticksForDays = TimeSpan.TicksPerDay * version.Build; // days since 1 January 2000
                    var ticksForSeconds = TimeSpan.TicksPerSecond * 2 * version.Revision; // seconds since midnight, (multiply by 2 to get original)
                    _CachedCurrentVersionDate = new DateTime(2000, 1, 1).Add(new TimeSpan(ticksForDays + ticksForSeconds)).ToString();
                }
    
                return _CachedCurrentVersionDate;
            }
            catch
            {
                return "Unknown Version Date";
            }
        }
    }
    

    This allows consumption as follows in your footer:

    Version: <%= Html.CurrentVersion() %> from <%= Html.CurrentVersionDate() %>
    
    0 讨论(0)
  • 2021-02-02 09:10

    That's because Assembly.GetEntryAssembly() is returning null: there is no "entry" assembly in an ASP.NET site (because the .NET framework is hosted in the w3wp.exe process). Assembly.GetEntryAssembly() is used to get the .exe assembly that you launched from (usually in a console or Windows application)

    The reason Assembly.GetAssembly(this.GetType()) is returning an assembly with version "0.0.0.0" is because ASP.NET compiles your Site.Master file into a temporary assembly under your "ASP.NET Temporary Files" folder. this is a reference to the "generated" class.

    Assembly.GetExecutingAssembly() is basically the same as Assembly.GetAssembly(this.GetType()) (except it also works when there is no "this" (e.g. in static methods).

    The best way would be use explicity use a type that you know exists in the assembly you're after. As an example, I assume your "Site.Master" has a code-behind file that is compiled into the assembly. You can use that instead:

    Assembly.GetAssembly(typeof(Site)).GetName().Version.ToString()
    

    (assuming the name of the class is Site)

    0 讨论(0)
  • 2021-02-02 09:15

    You can:

    e.g in your Application_Start method in Global.asax file add

    protected void Application_Start(object sender, EventArgs e)
    {
        HttpContext.Current.Application.Add("Version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
    }
    

    in HTML show it by

    <div><% =HttpContext.Current.Application["Version"].ToString() %></div>
    

    ALSO Change Assembly version to 1.0.0.* by going to - Project properties > Application > Assembly Information and assembly version is shown as 1.0.0.0 - change it to 1.0.0.*

    this will give you some versioning

    0 讨论(0)
  • 2021-02-02 09:22

    If you already have Global.asax in place, it could be a good place to store version globally once.

    Global.asax.cs:

    public class Global : HttpApplication
    {
        public static readonly Version Version = Assembly.GetExecutingAssembly().GetName().Version;
    }
    

    Your view:

    <div>- Version: @YourNamespace.Global.Version</div>
    
    0 讨论(0)
提交回复
热议问题