Retrieve Application Version in ASP Net Core 2.0 MVC
When you call typeof(RuntimeEnvironment).Assembly
, you're querying the containing assembly of that type. In this case this would be System.Runtime.InteropServices.dll
or Microsoft.Dotnet.PlatformAbstractions.dll
, depending on the namespace you've imported.
To get the information of your own assembly, you could simply replace RuntimeEnvironment
with one of your own types, for example
var appVersion = typeof(Program).Assembly
.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
or even
var appVersion = typeof(HomeController).Assembly
.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
This would return "6.6.7.0" if the Package version if your project is set as follows:
You were close!
Here you can find more information on reflection for .NET in general, but it should work fine for .NET Core.