如何检索产品版本?

pai*_*yff 2 c# versioning asp.net-core-mvc asp.net-core

有没有办法检索ASP.NET 5 Web应用程序的产品版本?

这就是我的意思project.json:

"version": "4.0.0-alpha1"
Run Code Online (Sandbox Code Playgroud)

我怎样才能从应用程序中检索它?我曾经能够在较旧的ASP.NET版本上执行此操作:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
Run Code Online (Sandbox Code Playgroud)

但是,现在它只是给了我0.0.0.0一些时间.有任何想法吗?

Wil*_*Ray 6

System.Reflection如果您还没有引用,请在project.json文件中添加引用.

"dependencies": {
    "System.Reflection": "4.1.0-beta-23516" // Current version at time of posting
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以从AssemblyInformationalVersionAttribute InformationalVersion属性中获取值.

private static string GetRuntimeVersion() =>
        typeof(SomeClassInYourAssembly)
            .GetTypeInfo()
            .Assembly
            .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
            .InformationalVersion;
Run Code Online (Sandbox Code Playgroud)