david
david

Reputation: 31

How can I read version of project in dotnet core (formerly asp.net mvc6)

how can I read assembly version information for my project, the value which in this case comes from the from project.json, and read that in my ASP.net core Controller and pass it to the view?

Upvotes: 3

Views: 1099

Answers (4)

david
david

Reputation: 31

In an early beta version you could add constructor to your controller with parameter IApplicationEnvironment. this param have a property with Version name

  public HomeController(IApplicationEnvironment appEnvironment) {
        this.appEnvironment = appEnvironment;
  }

(No longer works in ASP.net core 1.0)

Upvotes: -1

Warren  P
Warren P

Reputation: 68862

To get your application version, as it exists in project.json, you would write:

    string appVersion = Assembly.
      GetEntryAssembly().
     GetCustomAttribute<AssemblyInformationalVersionAttribute>().
     InformationalVersion;

Additionally, if you want to know which version of .net core your app is running on you can do this:

....
  string dotNetRuntimeVersion = typeof(RuntimeEnvironment)
                        .GetTypeInfo()
                        .Assembly
                        .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
                        .InformationalVersion;

You may need to add these units to your using for the above runtime version snippet:

using Microsoft.DotNet.InternalAbstractions;
using System.Reflection;

Upvotes: 1

Timothy Klenke
Timothy Klenke

Reputation: 824

You can use Razor to get this right from the View and bypass the Controller.

    <b>Version</b> @(
            Assembly.GetAssembly(typeof(HomeController))
                .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
                .InformationalVersion
        ) 

Upvotes: 1

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28425

You can get the AssemblyInformationalVersionAttribute from your project's assembly and then pass it to the view.

Here's how to get that attribute: https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs#L35-L44

Upvotes: 0

Related Questions