【问题标题】:Display project version in ASP.NET Core 1.0.0 web application在 ASP.NET Core 1.0.0 Web 应用程序中显示项目版本
【发布时间】:2017-01-31 16:29:33
【问题描述】:

过去在 RC.x 中工作的所有功能都不再有用了。

我试过这些:

  1. PlatformServices.Default.Application.ApplicationVersion;

  2. typeof(Controller).GetTypeInfo().Assembly.GetCustomAttribute().Version;

  3. Assembly.GetEntryAssembly().GetName().Version.ToString();

它们都返回 1.0.0.0 而不是 1.0.0-9,这应该是在执行 dotnet publish --version-suffix 9 之后在 project.json: "version": "1.0.0-*" 中包含这个

基本上,他们给我的是附图中的“文件版本”,而不是dotnet publish 实际上似乎发生变化的“产品版本”。

【问题讨论】:

    标签: version .net-assembly .net-core


    【解决方案1】:

    对于 1.x 版:

    Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
    

    对于 2.0.0 版本,此属性包含一些丑陋的东西: 2.0.0 built by: dlab-DDVSOWINAGE041 所以用这个:

    typeof(RuntimeEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
    

    【讨论】:

    • 谢谢,这很有帮助。为什么 asp.net 核心团队不支持 1.0.0.* 而不是 1-* 超出了我的理解。 Microsoft .NET 程序集的版本号一直是 int.int.int.int 并且从编程的角度来看是有意义的。在构建 # 中支持字符串不是必需的,并且会导致其他问题。
    • 改为使用 System.Reflection.IntrospectionExtensions.GetTypeInfo( typeof(ArmoredOutputStream) ) .Assembly.GetCustomAttribute().Version; // 注意:其中 ArmoredOutputStream 是程序集中您想要的版本的类
    • 编辑具有误导性,可能是由于编辑器的一些自定义设置。正确答案是 Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion FileVersion 删除预发布标签
    • 另外请注意 GetEntryAssembly,因为它可能不是您所期望的。在某些情况下,您可能更喜欢 GetExecutingAssembly() 或从某些已知类型中获取它。使用 typeof(RuntimeEnvironment).GetTypeInfo().Assembly 当然会给你一些系统程序集版本号...
    • 对于 .net core 2.2,Assembly.GetEntryAssembly().GetCustomAttribute&lt;AssemblyInformationalVersionAttribute&gt;().InformationalVersion; 似乎返回了正确的产品版本
    【解决方案2】:

    这对我也有用:

    @Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion
    

    它适用于 csproj 文件 - 1.2.3.4 或 1.2.3。但是 没有像this doc 所说的那样被识别。

    【讨论】:

    • 不,这总是返回 1.0.0.0。
    • 这是更好的答案。适用于项目属性。 (VS 2017)
    • 自 asp.net core 2.x 起已过时。见github.com/aspnet/Announcements/issues/237
    • 在 Core 3.1 / VS2019 中为我工作
    【解决方案3】:

    我会在 ASP.NET Core 2.0+ 上这样做

    var assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();
    

    【讨论】:

    • 虽然评分最低,但这个对我有用,在项目属性 => 包 => 程序集(文件)版本下配置版本。返回所有 4 个版本部分。
    • 这也是对我有用的解决方案。似乎 .Net Core 2 MVC 应用程序中的 RuntimeEnvironment 不包含正确的功能,但 Startup 包含。
    • 愚蠢的问题,但这是否 100% 保证适用于执行库之外的其他引用库?我正在移植一个 .NET 框架 4.6-7 应用程序,该应用程序当前正在寻找每个 dll 的文件位置以获取文件版本。我只是想确保这是一个等效的替换,只要我选择一个类型仅位于该程序集中。
    【解决方案4】:

    在 .Net Core 3.1 中,我使用以下方法直接在视图中显示版本:

             @GetType().Assembly.GetName().Version.ToString()
    

    这显示了您在 csproj 文件中的程序集版本:

    <PropertyGroup>
      <TargetFramework>netcoreapp3.1</TargetFramework>
      <AssemblyVersion>4.0.0.0</AssemblyVersion>
      <FileVersion>2.2.2.2</FileVersion>
      <Version>4.0.0-NetCoreRC</Version>
    </PropertyGroup>
    

    如果您想在视图中显示“其他”文件版本或“信息”版本属性,请使用 System.Reflection 添加:

    using System.Reflection;
    .... bunch of html and stuff
    <footer class="main-footer">
            <div class="float-right hidden-xs">
                <b>Assembly Version</b> @(Assembly.GetEntryAssembly().GetName().Version)
                <b>File Version</b> @(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version)
                <b>Info Version</b> @(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion)
            </div>    
        </footer>
    

    请注意,添加 System.Reflection 后,原始 @GetType().Assembly.GetName().Version.ToString() 行返回 0.0.0.0,您需要使用 @Assembly.GetEntryAssembly().GetName() .版本

    有一篇博文here

    编辑:确保遵循版本字符串的正确命名约定。一般来说,他们需要以数字领先。如果不这样做,您的应用程序将构建,但当您尝试使用 NuGet 添加或恢复包时,您将收到错误消息,例如“anythingGoesVersion”不是有效的版本字符串。或者更神秘的错误:缺少必需的属性“名称”。输入文件:C:\Users..csproj.' 更多here:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多