2

I am trying to get the text (2018.09.06) of the Version attribute of my Asp.Net Core 2 MVC project, which is captured in the project's options dialog:

enter image description here

The value is contained in the ReleaseVersion element in project's .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RootNamespace>Project Foo</RootNamespace>
    <ReleaseVersion>2018.09.06</ReleaseVersion>
    <UserSecretsId>[GUID here]</UserSecretsId>
  </PropertyGroup>
  ...
</Project>

I've tried this:

var version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;

But it returns 1.0.0.0.

I'm using .Net Core 2.0 Framework.

Note: the answers to Display project version in ASP.NET MVC Core application (RC2) don't work.

craig
  • 25,664
  • 27
  • 119
  • 205
  • It's strange that the schema definition does not have a `ReleaseVersion` element . If I change the `ReleaseVersion` to `Version` , it will work . – itminus Sep 07 '18 at 03:12
  • I'm seeing the exact same problem here, I always get 1.0.0.0. I even dumped the assembly and did not find the ReleaseVersion anywhere in it. Did you manage to find a solution? – Alex Suzuki Nov 22 '18 at 10:04
  • I'm dealing with this right now, have you made any progress on this? In my case, and maybe yours, I have each project in my solution set to delegate versioning to the solution. As far as I can tell, that is what causes the ReleaseVersion attribute to show up and there doesn't appear any way to access the ReleaseVersion attribute from the csproj file. – saarrrr May 09 '19 at 18:01
  • I made an issue for this: https://github.com/dotnet/core/issues/2700 – saarrrr May 09 '19 at 18:45

2 Answers2

0

I added a /PropertyGroup/VersionPrefix element to my project's .csproj file:

<PropertyGroup>
  <VersionPrefix>1.19.2</VersionPrefix>
  <VersionSuffix></VersionSuffix>
</PropertyGroup>

Then get the value:

string Version = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttribute<System.Reflection.AssemblyInformationalVersionAttribute>().InformationalVersion.ToString()

The value cannot be set by the project's Options UI, as far as I can tell.

Honestly, I'm confused why there is a UI for a separate version number (the one referenced in my question) that can't be accessed by the API.

craig
  • 25,664
  • 27
  • 119
  • 205
0

See: https://github.com/dotnet/core/issues/2700#issuecomment-491025782

Add a Directory.Build.props file to your solution folder and manage the version there instead of through the UI.

<Project>
  <PropertyGroup>
    <VersionPrefix>1.19.2</VersionPrefix>
  </PropertyGroup>
</Project>

I tend to think the VS tooling should be better about this, but as long as I only have one place to manage this value this is a significant improvement over the alternative.

saarrrr
  • 2,754
  • 1
  • 16
  • 26