代码之家  ›  专栏  ›  技术社区  ›  Jaxidian

无法获取页脚的程序集版本

  •  29
  • Jaxidian  · 技术社区  · 14 年前

    我正在使用中提到的自动构建版本控制 this question (不是选定的答案,而是使用 [assembly: AssemblyVersion("1.0.*")] 技术)。我在MVC 2中的site.master文件的页脚中执行此操作。我的代码如下:

    <div id="footer">
        <a href="emailto:webmaster@foo.com">webmaster@foo.com</a> - Copyright © 2005-<%= DateTime.Today.Year.ToString() %>, foo LLC. All Rights Reserved.
        - Version: <%= Assembly.GetEntryAssembly().GetName().Version.ToString() %>
    </div>
    

    我得到的例外是 Object reference not set to an instance of an object 因为 GetEntryAssembly() 收益率 NULL . 我的其他选择也行不通。 GetCallingAssembly() 始终返回“4.0.0.0”和 GetExecutingAssembly() 始终返回“0.0.0.0”。当我查看我的DLL时,一切都在按我的预期进行版本控制。但我不知道如何访问它来显示在我的页脚中!!

    4 回复  |  直到 12 年前
        1
  •  58
  •   Dean Harding    14 年前

    那是因为 Assembly.GetEntryAssembly() 返回空值:那里 不是 ASP.NET站点中的“Entry”程序集(因为.NET框架宿主在w3wp.exe进程中)。 assembly.getEntryassembly()。 用于获取从中启动的.exe程序集(通常在控制台或Windows应用程序中)

    原因 Assembly.GetAssembly(this.GetType()) 返回版本为“0.0.0.0”的程序集是因为ASP.NET将网站.master文件编译到“ASP.NET临时文件”文件夹下的临时程序集中。 this 是对“生成的”类的引用。

    Assembly.GetExecutingAssembly() 基本上 一样 assembly.getassembly(this.getType()) (除了在没有“this”的情况下(例如在静态方法中)它也可以工作。

    最好的方法是明确使用 知道 存在于您所追求的程序集中。例如,我假设您的“site.master”有一个编译到程序集中的代码隐藏文件。你可以用它来代替:

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

    (假设类的名称是 Site )

        2
  •  15
  •   Jaxidian    14 年前

    就像人们可能感兴趣的另一个解决方案一样,我精心设计了这些帮助者来帮助解决这个问题:

    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";
            }
        }
    }
    

    这允许在页脚中使用以下内容:

    Version: <%= Html.CurrentVersion() %> from <%= Html.CurrentVersionDate() %>
    
        3
  •  8
  •   Manoj    12 年前

    你可以:

    例如,在应用程序中,在global.asax文件中添加

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

    在HTML中显示

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

    产科加强生命支持 将程序集版本更改为1.0.0.*转到 -项目属性>应用程序>程序集信息和程序集版本显示为1.0.0.0-将其更改为1.0.0。*

    这将为您提供一些版本控制

        4
  •  3
  •   Clay hemu    13 年前

    如果已经安装了global.asax,那么它可能是一个很好的全局存储版本的地方。

    Global.asax.cs:

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

    你的观点:

    <div>- Version: @YourNamespace.Global.Version</div>