Last active
August 29, 2015 14:17
-
-
Save jchandra74/d3a7e9726c963eab2b13 to your computer and use it in GitHub Desktop.
asp.net good config and practices
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
//Stolen from @mkristensen | |
//Use to bust static content like .js and .css when they changed. | |
using System.IO; | |
using System.Web; | |
using System.Web.Caching; | |
using System.Web.Hosting; | |
namespace CHANGE_YOUR_NAMESPACE_HERE | |
{ | |
public class Fingerprint | |
{ | |
public static string Tag(string rootRelativePath) | |
{ | |
if (HttpRuntime.Cache[rootRelativePath] == null) | |
{ | |
var absolute = HostingEnvironment.MapPath("~" + rootRelativePath); | |
var date = File.GetLastWriteTime(absolute); | |
var index = rootRelativePath.LastIndexOf('/'); | |
var result = rootRelativePath.Insert(index, "/v-" + date.Ticks); | |
HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute)); | |
} | |
return HttpRuntime.Cache[rootRelativePath] as string; | |
} | |
} | |
} |
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
<system.webserver> | |
<!-- ensure gzip on both static and javascript, etc. --> | |
<urlCompression doStaticCompression="true" doDynamicCompression="true" /> | |
<!-- long expiry cache for static contents --> | |
<staticContent> | |
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" /> | |
</staticContent> | |
<!-- url rewriting to strip of the versioning to break the long expiry cache on static contents --> | |
<rewrite> | |
<rules> | |
<rule name="fingerprint"> | |
<match url="([\S]+)(/v-[0-9]+/)([\S]+)" /> | |
<action type="Rewrite" url="{R:1}/{R:3}" /> | |
</rule> | |
</rules> | |
</rewrite> | |
</system.webserver> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment