Search code examples
c#asp.net-mvcasp.net-core.net-core-rc2

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


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'm trying:

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

This does not work, it displays "1.0.0" instead of real value from project.json

I also tried this but it looks like it is no longer works in RC2:

@inject Microsoft.Extensions.PlatformAbstractions.IApplicationEnvironment appEnv
My version number is @(appEnv.ApplicationVersion)

Solution

  • 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.