c# - 如何检索产品版本?

标签 c# asp.net-core versioning asp.net-core-mvc

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

这是我的 project.json 中的内容:

"version": "4.0.0-alpha1"

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

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

但是,现在它一直给我 0.0.0.0。有什么想法吗?

最佳答案

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

"dependencies": {
    "System.Reflection": "4.1.0-beta-23516" // Current version at time of posting
}

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

private static string GetRuntimeVersion() =>
        typeof(SomeClassInYourAssembly)
            .GetTypeInfo()
            .Assembly
            .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
            .InformationalVersion;

关于c# - 如何检索产品版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35903625/

相关文章:

c# - 使用 DI 容器和对象实例解析对象

jsf-2 - 如何对 jar 中的资源使用 JSF 版本控制

c# - 从外部库提供静态文件

asp.net-core - 无法在 AWS EC2 Ubuntu 上运行 ASP.NET Core 应用程序

rest - 版本控制 RESTful 服务?

c# - 将引用托管代码中分配的内存的指针传递给非托管代码

c# - OAuthException : (#200) The user hasn't authorized the application to perform this action

c# - 只要没有单击任何项​​目,就阻止 ToolStripDropDownButton 关闭

c# - Entity Framework Core 3.1 中的 LINQ group by