我如何阅读dotnet核心中的项目版本(以前的asp.net mvc6)

时间:2015-10-11 09:28:45

标签: asp.net-core asp.net-core-mvc

如何读取项目的汇编版本信息,在这种情况下,值来自from project.json,并在我的ASP.net核心控制器中读取并将其传递给视图?

4 个答案:

答案 0 :(得分:1)

您可以使用Razor从View中获取此功能并绕过Controller。

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

答案 1 :(得分:1)

要获得应用程序版本(如project.json中所示),您可以编写:

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

此外,如果您想知道您的应用运行的.net核心版本,您可以这样做:

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

您可能需要将这些单位添加到您使用的上述运行时版本代码段中:

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

答案 2 :(得分:0)

您可以从项目的程序集中获取AssemblyInformationalVersionAttribute,然后将其传递给视图。

以下是获取该属性的方法:https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs#L35-L44

答案 3 :(得分:-1)

在早期测试版中,您可以使用参数IApplicationEnvironment向控制器添加构造函数。此参数具有Version名称

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

(不再适用于ASP.net核心1.0)

相关问题