Skip to content

Instantly share code, notes, and snippets.

@oledid
Last active November 1, 2019 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oledid/00c609261b9440b6645d106f03d01118 to your computer and use it in GitHub Desktop.
Save oledid/00c609261b9440b6645d106f03d01118 to your computer and use it in GitHub Desktop.
BuildDateAttribute - get build-date in build for cache-busting
// https://www.meziantou.net/getting-the-date-of-build-of-a-dotnet-assembly-at-runtime.htm
using System;
using System.Globalization;
namespace Core
{
[AttributeUsage(AttributeTargets.Assembly)]
public class BuildDateAttribute : Attribute
{
public BuildDateAttribute(string value)
{
DateTime = DateTime.ParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None);
}
public DateTime DateTime { get; }
}
}
// [...]
@{
var buildDateAttr = typeof(Web.Startup).Assembly.GetCustomAttributes(typeof(Core.BuildDateAttribute), true).FirstOrDefault() as Core.BuildDateAttribute;
var date = buildDateAttr != null ? buildDateAttr.DateTime : (DateTime?)null;
Context.Items["BuildNumber"] = (date == null ? DateTime.Now : date).Value.ToString("yyyyMMddHHmmss");
}
<link rel="stylesheet" href="@(Url.Content("~/libs/bootstrap/css/bootstrap.min.css") + "?c=" + Context.Items["BuildNumber"])">
// [...]
<!-- [...] -->
<ItemGroup>
<AssemblyAttribute Include="Core.BuildDateAttribute">
<_Parameter1>$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<!-- [...] -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment