Display project version in ASP.NET Core 1.0.0 web application
Asked Answered
P

5

26

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

Pietrek answered 22/9, 2016 at 23:2 Comment(0)
P
32

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;
Pietrek answered 26/9, 2016 at 2:8 Comment(7)
Thanks, that is very helpful. Why asp.net core team didn't support 1.0.0.* instead of 1-* is beyond me. The version numbers for Microsoft .NET assemblies have always been int.int.int.int and makes sense from a programmatic standpoint. Supporting a string in a build # is not necessary and leads to other issues.Tullis
Instead, use System.Reflection.IntrospectionExtensions.GetTypeInfo( typeof(ArmoredOutputStream) ) .Assembly.GetCustomAttribute<AssemblyVersionAttribute>().Version; // Note: where ArmoredOutputStream is a class in the assembly whose version you wantCestus
The edit is misleading and probably due to some customized settings from the editor. The correct answer is Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion FileVersion drops the prerelease tagsCatharina
In addition be mindful about GetEntryAssembly as it might not be what you expect. I some cases you might prefer GetExecutingAssembly() or getting it from some known type. Using typeof(RuntimeEnvironment).GetTypeInfo().Assembly will of course give you some system assembly version number...Catharina
@Tullis re: int.int.int.int The AssemblyVersion and AssemblyFileVersion are defined to be just 4-ints, but the AssemblyInformationalVersion is defined as a string and may have the - and words etc.Tivoli
for .net core 2.2, Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion; seem to be returning the correct Product versionSlightly
@Abhinav Galodha Looks like you have the correct answer!Kalmick
H
26

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

var assemblyVersion = typeof(Startup).Assembly.GetName().Version.ToString();
Hype answered 14/3, 2018 at 20:29 Comment(3)
Although lowest rated, this one is working for me, configured version under project properties => Packages => Assembly (file) version. Returns all 4 version parts.Donate
This is also the solution that worked for me. Seems the RuntimeEnvironment in .Net Core 2 MVC application does not contain the right functions, but Startup do.Homologate
Silly question, but is this 100% guaranteed to work for other referenced libraries outside of the executing one? I'm porting a .NET framework 4.6-7 app which is currently looking for file locations of each dll in order to get the file versions.. I just wanted to make sure this is an equivalent replacement as long as I choose a type that is only located in that assembly.Assonance
T
6

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:

Tennilletennis answered 23/9, 2020 at 18:45 Comment(1)
Actually this seems to be the correct working one. It shows the version number I set (2.0.1 for me) in the Package tab. This was what I was looking for and it is just fine. The accepted answer shows crap : "4.700.21.11602"Mac
T
4

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.

Tantalite answered 30/3, 2017 at 22:12 Comment(4)
Nah, this return 1.0.0.0 always.Cestus
It is better answare. Works with project properties. (VS 2017)Mosely
Obsolete as of asp.net core 2.x. See github.com/aspnet/Announcements/issues/237Craquelure
Works for me in Core 3.1 / VS2019Blasphemy
M
0

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 !!!

Mac answered 7/11, 2022 at 6:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.