Cannot get assembly version for footer

前端 未结 4 1463
北荒
北荒 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;
    
        /// 
        /// Return the Current Version from the AssemblyInfo.cs file.
        /// 
        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() %>
    

提交回复
热议问题