2016-09-22 59 views
11

以前在RC.x中工作的内容都不再有用。在ASP.NET Core 1.0.0 web应用程序中显示项目版本

我尝试了这些:

  1. PlatformServices.Default.Application.ApplicationVersion;

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

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

他们都返回1.0.0.0代替1.0.0-9这应该是在project.json: "version": "1.0.0-*"

基本上有这个dotnet publish --version-suffix 9执行后,他们给我的“文件版本”从附加的图片,而不是“产品版本“dotnet publish实际上似乎改变了。

enter image description here

回答

18

对于1.x版本:

Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion; 

对于版本2.0.0这个属性包含丑陋的东西: 2.0.0 built by: dlab-DDVSOWINAGE041所以用这一个:

typeof(RuntimeEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version; 
+0

谢谢,这是非常有帮助的。为什么asp.net核心团队不支持1.0.0 *,而不是1- *超越了我。 Microsoft .NET程序集的版本号一直是int.int.int.int,从编程的角度来看是有意义的。在构建#中支持字符串不是必需的,会导致其他问题。 – evermeire

+0

而是使用System.Reflection.IntrospectionExtensions.GetTypeInfo( typeof(ArmoredOutputStream) ) .Assembly.GetCustomAttribute ()。Version; //注意:其中ArmoredOutputStream是您想要的版本的程序集中的类 –

+0

编辑具有误导性,可能是由于编辑器的某些自定义设置。正确答案是Assembly.GetEntryAssembly()。GetCustomAttribute ()。InformationalVersion FileVersion删除预发布标签 –

5

这工作我也是:

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

它可用于csproj文件 - <版本> 1.2.3.4或< VersionPrefix> 1.2.3 </VersionPrefix>。然而,< VersionSuffix>并没有被识别为this doc说。

+6

Nah,总是返回1.0.0.0。 –

+0

这是更好的answare。适用于项目属性。 (VS 2017) – harveyt

相关问题