Created
October 21, 2016 07:26
-
-
Save skttl/ba4cfec43026cfb0787eff51813cc0ae to your computer and use it in GitHub Desktop.
Adds the last write time of a file, to a querystring parameter, for busting local caches. Usage:
@UrlHelpers.CachebustPath("~/path/to/styles.css")
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
using Umbraco.Core; | |
namespace MyWebsite.Core.Helpers | |
{ | |
public static class UrlHelpers | |
{ | |
/// <summary> | |
/// Takes a path, and adds a querystring value matching the last write time, to bust eventual browser caching. | |
/// </summary> | |
/// <param name="path"></param> | |
/// <returns></returns> | |
public static string CachebustPath(string path) | |
{ | |
var file = System.Web.HttpContext.Current.Server.MapPath(path); | |
if (System.IO.File.Exists(file)) | |
{ | |
return path + "?" + System.IO.File.GetLastWriteTime(file).Ticks.ToString().ToUrlSegment(); | |
} | |
else | |
{ | |
return path + "?404"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment