如何读取 dotnet 核心中的项目版本(以前的 asp.net mvc6)
How can I read version of project in dotnet core (formerly asp.net mvc6)
我如何读取项目的程序集版本信息,在本例中的值来自 project.json,并在我的 ASP.net 核心控制器中读取并将其传递给视图?
您可以从项目的程序集中获取 AssemblyInformationalVersionAttribute
,然后将其传递给视图。
获取该属性的方法如下:https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs#L35-L44
在早期的测试版中,您可以使用参数 IApplicationEnvironment
向控制器添加构造函数。这个参数有一个 属性 和 Version
name
public HomeController(IApplicationEnvironment appEnvironment) {
this.appEnvironment = appEnvironment;
}
(不再适用于 ASP.net 核心 1.0)
您可以使用 Razor 从视图中获得此权利并绕过控制器。
<b>Version</b> @(
Assembly.GetAssembly(typeof(HomeController))
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion
)
要获取您的应用程序版本,因为它存在于 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;
我如何读取项目的程序集版本信息,在本例中的值来自 project.json,并在我的 ASP.net 核心控制器中读取并将其传递给视图?
您可以从项目的程序集中获取 AssemblyInformationalVersionAttribute
,然后将其传递给视图。
获取该属性的方法如下:https://github.com/aspnet/dnx/blob/dev/src/Microsoft.Dnx.Host/RuntimeEnvironment.cs#L35-L44
在早期的测试版中,您可以使用参数 IApplicationEnvironment
向控制器添加构造函数。这个参数有一个 属性 和 Version
name
public HomeController(IApplicationEnvironment appEnvironment) {
this.appEnvironment = appEnvironment;
}
(不再适用于 ASP.net 核心 1.0)
您可以使用 Razor 从视图中获得此权利并绕过控制器。
<b>Version</b> @(
Assembly.GetAssembly(typeof(HomeController))
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion
)
要获取您的应用程序版本,因为它存在于 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;