在 ASP.NET 核心 1.0.0 Web 应用程序中显示项目版本

Display project version in ASP.NET Core 1.0.0 web application

None 曾经在 RC.x 中工作的东西现在有用了。

我试过这些:

  1. PlatformServices.Default.Application.ApplicationVersion;

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

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

它们都是 return 1.0.0.0 而不是 1.0.0-9 应该在 dotnet publish --version-suffix 9 执行之后 project.json: "version": "1.0.0-*"

基本上他们给我的是 "File version" 所附图片而不是 "Product version",dotnet publish 实际上似乎有所改变。

版本 1.x:

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

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

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

这对我也有用:

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

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

我会在 ASP.NET Core 2.0+

上这样做
var assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();

在 .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() 行 returns 0.0.0.0 之后,您需要使用 @Assembly.GetEntryAssembly().GetName().Version

有博客posthere

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