Skip to content

Instantly share code, notes, and snippets.

@alexcurtis
Created February 25, 2013 10:47
MVC Scripts + Styles With Cache Buster
public static class ViewComponentHelper
{
private static string _cacheComponentVersion;
public static IHtmlString RenderScripts(string[] paths)
{
//Bust The Cache.
var bustedPaths = paths.Select(BustPath).ToArray();
return Scripts.Render(bustedPaths);
}
public static IHtmlString RenderStyles(string[] paths)
{
//Bust The Cache.
var bustedPaths = paths.Select(BustPath).ToArray();
return Styles.Render(bustedPaths);
}
private static string BustPath(string path)
{
var cachedComponentVersion = GetCachedComponentVersion();
return path + "?" + cachedComponentVersion;
}
private static string GetCachedComponentVersion()
{
const string cacheKey = "CacheComponentVersion";
return _cacheComponentVersion ??
(_cacheComponentVersion = ConfigurationManager.AppSettings[cacheKey]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment