How display application version in 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 -
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
126.96.36.199, 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.
$(BUILD_BUILDNUMBER) variable is available, then it will take that otherwise (local dev environment) it will display like
$(BUILD_BUILDNUMBER) will be populated like
Year,Month,Day of Month and Build number.
Happy Programming :)