I am developing a web site using asp.net core.
And I publish it with Visual Studio
or/and VSTS
.
I want to display some information about which build it is on the web page.
(something like rev 2016.9.20.4002
)
How can I do that?
You can track it with build number.
Steps to display build number to your website:
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\\\mssqllocaldb;Database=aspnet-WebApplication1-ab933d83-8f4b-4024-9f3c-1aef5339a8f3;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } }, "CodeVersion": { "Num": "#{MyBuildNumber}#" } }
Looks like ApplicationEnvironment class is what you need:
var appEnv = new Microsoft.Extensions.PlatformAbstractions.ApplicationEnvironment();
string version = appEnv.ApplicationVersion;
Also How can I auto-increment an MVC 6 version number? may be also interesting to you, but keep in mind, that IApplicationEnvironment
has been removed.
Do you mean something like this:
In project.json:
{
"title": "Your Application name",
"version": "2016.9.20.4002",
"copyright": "Your Company 2016",
"description": "Awesome ASP.Net Core Application",
"dependencies": {
//rest of project.json
You can then create a property in your view model or model such as:
public static string Version
{
get
{
var assembly = Assembly.GetExecutingAssembly();
var fileVersion = GetCustomAttribute<AssemblyFileVersionAttribute>(assembly);
return fileVersion?.Version;
}
}
In your view:
@model Namespace.CustomViewModel
<!--Other HTML Code-->
<span id="applicationVersion">@CustomViewModel.Version</span>
Just as an alternative option, you could read the time that the assembly was created and display it in a version format. Every time the assembly is rebuilt, this value would change to the time it was created.
(adapted from this answer for .Net Core)
public static class AppInfo
{
private static Lazy<string> buildVersion =
new Lazy<string>(() => GetBuildVersion(Assembly.GetEntryAssembly()));
public static string BuildVersion { get; } = buildVersion.Value;
private static string GetBuildVersion(Assembly assembly)
{
var filePath = assembly.Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;
var buffer = new byte[2048];
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
stream.Read(buffer, 0, 2048);
var offset = BitConverter.ToInt32(buffer, c_PeHeaderOffset);
var secondsSince1970 = BitConverter.ToInt32(buffer, offset + c_LinkerTimestampOffset);
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var linkTimeUtc = epoch.AddSeconds(secondsSince1970);
var localTime = TimeZoneInfo.ConvertTime(linkTimeUtc, TimeZoneInfo.Local);
var minutesFromMidnight = localTime.Minute + localTime.Hour * 60;
return localTime.ToString("yyyy.M.dd.") + minutesFromMidnight;
}
}
Then just reference it in Razor as:
@AppInfo.BuildVersion
After a day of research, finally found/created a better option than using any random app (Replace Token) from Marketplace.
The option I am talking is already available in VSTS, Azure CLI task.
Here are the stpes:
az webapp config appsettings set -n iCoreTestApi -g ArchitectsSandbox -s Dev --settings BUILD_NUMBER=$(Build.BuildNumber)
Command explanation:
Once you will queue new build, after successful completion of the deployment, you can see app settings section on Azure is updated with new BUILD_NUMBER.
Let me know if you still have any question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.