rook
rook

Reputation: 3008

Display project version in ASP.NET Core 1.0.0 web application

None of what used to work in RC.x helps anymore.

I have tried these:

  1. PlatformServices.Default.Application.ApplicationVersion;

  2. typeof(Controller).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;

  3. Assembly.GetEntryAssembly().GetName().Version.ToString();

They all return 1.0.0.0 instead of 1.0.0-9 which should be after execution of the dotnet publish --version-suffix 9 having this in project.json: "version": "1.0.0-*"

Basically they give me "File version" from the attached picture instead of "Product version" which dotnet publish actually seems to change.

enter image description here

Upvotes: 25

Views: 20670

Answers (5)

Venugopal M
Venugopal M

Reputation: 2411

The answer by Michael G should have been the accepted one since it works as expected. Just citing the answer by Michael G above.

var version = GetType().Assembly.GetName().Version.ToString();

works fine. It gets the Package version set in the Package tab of project properties. As an addition, if we need to get the Description we set in the same tab, this code would work. (core 3.1)

string desc = GetType().Assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;

Just in case someone needs this. Happy coding !!!

Upvotes: 0

Michael G
Michael G

Reputation: 582

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:

Upvotes: 6

CREM
CREM

Reputation: 1991

I would do it like this on ASP.NET Core 2.0+

var assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();

Upvotes: 26

rook
rook

Reputation: 3008

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;

Upvotes: 31

Calvin
Calvin

Reputation: 1163

This work for me too:

@Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationVersion

It works with csproj file - either <Version>1.2.3.4, or <VersionPrefix>1.2.3</VersionPrefix>. However the <VersionSuffix> isn't recoganized as this doc says.

Upvotes: 4

Related Questions