Search code examples
c#asp.net-coreversioningasp.net-core-mvc

How to retrieve Product Version?


Is there a way to retrieve the product version of an ASP.NET 5 web application?

This is what I have in my project.json:

"version": "4.0.0-alpha1"

How would I be able to retrieve this from within the application? I used to be able to do this on older ASP.NET versions:

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

However, now it just gives me 0.0.0.0 all the time. Any ideas?


Solution

  • Inject IApplicationEnvironment anywhere you need the version. So for instance in the Configure method of the Startup class:

           public void Configure(IApplicationBuilder app, 
            IApplicationEnvironment applicationEnvironment)
        {
            app.UseIISPlatformHandler();
    
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync(applicationEnvironment.ApplicationVersion);
            });
        }
    

    Source: "Services Available in Startup" http://docs.asp.net/en/latest/fundamentals/startup.html