Skip to content

Instantly share code, notes, and snippets.

@theorigin
Last active August 29, 2015 13:57
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 theorigin/9350268 to your computer and use it in GitHub Desktop.
Save theorigin/9350268 to your computer and use it in GitHub Desktop.
Add a datetime suffix to style/script tags in HTML to force a refresh when they change
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta content="" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<title>Example</title>
<link rel="stylesheet" href="@Url.ContentWithHash("reset.css")" />
<script type="text/javascript" src="@Url.ContentWithHash("jquery-1.4.4.js")"></script>
... becomes ...
<link rel="stylesheet" href="reset.css?20131309143026" />
<script type="text/javascript" src="jquery-1.4.4.js?20130410134656"></script>
</head>
<body>
</body>
public static class UrlHelperExtensions
{
public static string ContentWithHash(this UrlHelper urlHelper, string contentPath)
{
var virtualFile = urlHelper.Content(contentPath);
var actualFile = urlHelper.RequestContext.HttpContext.Server.MapPath(contentPath);
var creationDateTime = (File.Exists(actualFile) ? File.GetLastWriteTimeUtc(actualFile) : new DateTime()).ToString("yyyyddMMHHmmss");
return string.Format("{0}?{1}", virtualFile, creationDateTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment