Skip to content

Instantly share code, notes, and snippets.

@jchandra74
Last active August 29, 2015 14:17
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 jchandra74/d3a7e9726c963eab2b13 to your computer and use it in GitHub Desktop.
Save jchandra74/d3a7e9726c963eab2b13 to your computer and use it in GitHub Desktop.
asp.net good config and practices
//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;
}
}
}
<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