Asp.net-Core-Mvc

在 ASP Net Core 2.0 MVC 中檢索應用程序版本

  • December 5, 2019

我正在完成在 Win10 上使用 .net core 2.0 和 vs2017 建構一個 mvc Web 應用程序。在編寫“關於”頁面時,我希望輸入目前項目版本號(目前仍設置為 1.0.0)。我會認為這很簡單!

我能找到的唯一參考建議:

AppVersion = typeof(RuntimeEnvironment).GetTypeInfo ().Assembly
   .GetCustomAttribute<AssemblyFileVersionAttribute> ().Version;

但是,在我的情況下,這會返回“4.6.25814.01”——這不是必需的。

任何人都可以建議如何在程式碼中檢索版本嗎?

我假設我想要“包版本”,但承認我不清楚/如何使用“包版本”、“程序集版本”和“程序集文件版本”之間的區別。

當您呼叫 時typeof(RuntimeEnvironment).Assembly,您正在查詢該類型的包含程序集。在這種情況下,這將是System.Runtime.InteropServices.dllMicrosoft.Dotnet.PlatformAbstractions.dll,具體取決於您導入的命名空間。

要獲取您自己的程序集的資訊,您可以簡單地替換RuntimeEnvironment為您自己的類型之一,例如

var appVersion = typeof(Program).Assembly
   .GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

甚至

var appVersion = typeof(HomeController).Assembly
   .GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

如果您的項目設置如下,則如果包版本將返回“6.6.7.0”:

在此處輸入圖像描述

你很親近!

在這裡,您可以找到有關 .NET 反射的更多資訊,但它應該適用於 .NET Core。

引用自:https://stackoverflow.com/questions/48081831