Display project version in ASP.NET Core 1.0.0 web application
In .Net Core 3.1 I show the version directly in my View using:
@GetType().Assembly.GetName().Version.ToString()
This shows the Assembly Version you have in your csproj file:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<FileVersion>2.2.2.2</FileVersion>
<Version>4.0.0-NetCoreRC</Version>
</PropertyGroup>
If you want to display the "other" FileVersion or "Informational" Version properties in the View add using System.Reflection:
using System.Reflection;
.... bunch of html and stuff
<footer class="main-footer">
<div class="float-right hidden-xs">
<b>Assembly Version</b> @(Assembly.GetEntryAssembly().GetName().Version)
<b>File Version</b> @(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version)
<b>Info Version</b> @(Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion)
</div>
</footer>
Note that after adding the System.Reflection the original @GetType().Assembly.GetName().Version.ToString() line returns 0.0.0.0 and you need to use the @Assembly.GetEntryAssembly().GetName().Version
There's a blog post here
Edit: Make sure to follow proper naming conventions for the Version strings. In general, they need to lead with a number. If you don't, your app will build but when you try to use NuGet to add or restore packages you'll get an error like 'anythingGoesVersion' is not a valid version string. Or a more cryptic error: Missing required property 'Name'. Input files: C:\Users....csproj.' more here:
I would do it like this on ASP.NET Core 2.0+
var assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();
For version 1.x:
Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
For version 2.0.0 this attribute contains something ugly:
2.0.0 built by: dlab-DDVSOWINAGE041
so use this one:
typeof(RuntimeEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;