Skip to content

Instantly share code, notes, and snippets.

@andrewabest
Created December 16, 2014 04:25
Show Gist options
  • Save andrewabest/68ff8edac4017d54f195 to your computer and use it in GitHub Desktop.
Save andrewabest/68ff8edac4017d54f195 to your computer and use it in GitHub Desktop.
Cache Busting in MVC local
public class AppendFileHash : IBundleTransform
{
public void Process(BundleContext context, BundleResponse response)
{
foreach (var file in response.Files)
{
using (var fs = File.OpenRead(HostingEnvironment.MapPath(file.IncludedVirtualPath)))
{
var hash = new SHA256Managed().ComputeHash(fs);
var version = HttpServerUtility.UrlTokenEncode(hash);
file.IncludedVirtualPath = string.Concat(file.IncludedVirtualPath, "?hbuster=", version);
}
}
}
}
public class ProductNameScriptBundle : ScriptBundle
{
public ProductNameScriptBundle(string virtualPath) : base(virtualPath)
{
if (AppEnvironment.IsLocal())
Transforms.Add(new AppendFileHash());
}
public ProductNameScriptBundle(string virtualPath,
string cdnPath) : base(virtualPath, cdnPath)
{
if (AppEnvironment.IsLocal())
Transforms.Add(new AppendFileHash());
}
}
public class ProductNameStyleBundle : StyleBundle
{
public ProductNameStyleBundle(string virtualPath) : base(virtualPath)
{
if (AppEnvironment.IsLocal())
Transforms.Add(new AppendFileHash());
}
public ProductNameStyleBundle(string virtualPath,
string cdnPath) : base(virtualPath, cdnPath)
{
if (AppEnvironment.IsLocal())
Transforms.Add(new AppendFileHash());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment