Last active
August 29, 2015 13:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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