Display project version in ASP.NET MVC Core application (RC2)

前端 未结 5 600
生来不讨喜
生来不讨喜 2020-12-11 00:41

How do I display application version from the project.json? I am using gulp-bump to autoincrement version, but I can\'t show the recent version. Here is what I\

相关标签:
5条回答
  • 2020-12-11 01:01

    Since Platform Abstractions were obly shipped with ASP.NET Core 1 and has been removed from ASP.NET Core 2 and up, if you're using version 2 or above, you must replace this row:

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

    with this one:

    System.Reflection.Assembly.GetEntryAssembly().GetName().Version
    

    as specified in "Replacing API usage" section of the previous linked page.

    0 讨论(0)
  • 2020-12-11 01:06

    I used a different approach, as stated in this answer which gave me a SemVer version (1.0.0) which is actually in my project.json and not 1.0.0.0, which is returned by accepted answer. So the code would be:

    var runtimeVersion = typeof(Startup)
                .GetTypeInfo()
                .Assembly
                .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
                .InformationalVersion;
    

    It returns correct suffixed versions as well, i.e. something like "2.0.1-dev01"

    0 讨论(0)
  • 2020-12-11 01:11

    As per this announcement, IApplicationEnvironment no longer exists.

    You can still access the ApplicationVersion statically using:

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

    It works for me. My project.json looks like this:

    {
        "version": "1.0.0.2",
        // all the rest
    }
    

    And in my index view, I have the following line at the top:

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

    And I correctly get 1.0.0.2 in the output. And when I change that value and restart (build) the application, the new version is shown there.

    0 讨论(0)
  • 2020-12-11 01:14

    This worked for me for .NET Core 2.0.5.

    Code:

    var assemblyVersion = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttribute<System.Reflection..AssemblyInformationalVersionAttribute>().InformationalVersion;
    Console.WriteLine(assemblyVersion);
    

    myproject.csproj

    <PropertyGroup>
        <VersionPrefix>1.0.0.1</VersionPrefix>
        <VersionSuffix>alpha</VersionSuffix>
    </PropertyGroup>
    

    outputs

    1.0.0.1-alpha
    
    0 讨论(0)
  • 2020-12-11 01:17

    If you end up here and you are trying to manage project versions through the solution version.

    Adding a Directory.Build.props file to the solution directory containing:

    <Project>
      <PropertyGroup>
        <VersionPrefix>1.2.3</VersionPrefix>
      </PropertyGroup>
    </Project>
    

    Will set the Version property of all included projects that 'Get version from parent solution'.

    Not ideal, but it allows you to manage the version in a single place for a version locked multi-project solution.

    0 讨论(0)
提交回复
热议问题