How display application version in ASP.NET Core
November 25, 2018 by Anuraj
ASP.NET Core
Most enterprise application soon or latter, have a requirement to trace out the version of a currently running application. This post shows how to display application version in a web app and how to increment it as part of build process.
You can get the version of your app in ASP.NET Core 2.x like this - Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion
I implemented it as a service and the service will return a string property version, which can be injected to the views.
And here is the implementation.
Next you can inject the service to the controllers and views.
And here is the code in the view - _ViewImports.cshtml
And you can use it in the view like this.
Which will displayed like this.
By default it will display version 1.0.0.0
, if you want to modify it you can add version element with required version info like this.
One of the common requirement is updating the version number while deploying to QA / Production environments, to do this you can use some CI configuration variables. For Azure DevOps, you can use $(BUILD_BUILDNUMBER)
variable. And you can configure it from Options > Build Properties.
And you need to modify your project code like this.
If the $(BUILD_BUILDNUMBER)
variable is available, then it will take that otherwise (local dev environment) it will display like 1.2.3.4-Dev
. The $(BUILD_BUILDNUMBER)
will be populated like Year,Month,Day of Month and Build number
.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub