Last active
August 29, 2015 14:03
-
-
Save normansolutions/5600e49943d22e5eb5df to your computer and use it in GitHub Desktop.
For use on blog post: http://normansolutions.co.uk/post/cache-busting-in-c-without-querystrings
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 string FingerPrint(string rootRelativePath, string cdnPath = "") | |
{ | |
if (!string.IsNullOrEmpty(cdnPath) && !HttpContext.Current.IsDebuggingEnabled) | |
{ | |
return cdnPath; | |
} | |
if (HttpRuntime.Cache[rootRelativePath] == null) | |
{ | |
string relative = VirtualPathUtility.ToAbsolute("~" + rootRelativePath); | |
string absolute = HostingEnvironment.MapPath(relative); | |
if (!File.Exists(absolute)) | |
{ | |
throw new FileNotFoundException("File not found", absolute); | |
} | |
DateTime date = File.GetLastWriteTime(absolute); | |
int index = relative.LastIndexOf('/'); | |
string result = relative.Insert(index, "/v-" + date.Ticks); | |
HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute)); | |
} | |
return HttpRuntime.Cache[rootRelativePath] as string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment