3

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?

4 Answers 4

1

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
        ) 
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

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

1 Comment

actually showing what to do is preferred on Stackoverflow instead of "read this link which is going to break later"
-1

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)

1 Comment

This answer is no longer correct, in later versions and indeed in the released version this does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.