Created
March 28, 2013 14:55
-
-
Save JoeDoyle23/5263778 to your computer and use it in GitHub Desktop.
A WebForms user control to add a query string to prevent browser caching when the file changes. Works with JavaScript files (script tags) and CSS stylesheets (link tags).
This file contains hidden or 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
/* | |
Usage: | |
For JavaScript Files | |
<asp:FileVersioner Path="~/js/plugins/jQuery-1.7.2.js" Type="script" runat="server"></asp:FileVersioner> | |
For CSS stylesheets | |
<asp:FileVersioner Path="~/stylesheets/CustomerPortal.css" Type="style" runat="server"></asp:FileVersioner> | |
*/ | |
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Security.Cryptography; | |
using System.Web.UI; | |
namespace Site.Controls | |
{ | |
public partial class FileVersioner : UserControl | |
{ | |
public string Type { get; set; } | |
public string Path { get; set; } | |
private const string _scriptTemplate = "<script src=\"{0}?ver={1}\" type=\"text/javascript\"></script>"; | |
private const string _styleTemplate = "<link href=\"{0}?ver={1}\" rel=\"stylesheet\" type=\"text/css\" />"; | |
public FileVersioner() | |
{ | |
Type = string.Empty; | |
Path = string.Empty; | |
} | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
var checksum = GetChecksum(); | |
if (Type.ToLower() == "script") | |
{ | |
var scriptTag = new LiteralControl { Text = string.Format(_scriptTemplate, Path, checksum) }; | |
Controls.Add(scriptTag); | |
return; | |
} | |
if (Type.ToLower() == "style") | |
{ | |
var scriptTag = new LiteralControl { Text = string.Format(_styleTemplate, Path, checksum) }; | |
Controls.Add(scriptTag); | |
return; | |
} | |
throw new ArgumentException("The Type attribute must be 'script' or 'style'."); | |
} | |
private string GetChecksum() | |
{ | |
var physicalFile = MapPath(Path); | |
if (!File.Exists(physicalFile)) | |
return "0"; | |
var lastModified = File.GetLastWriteTime(physicalFile); | |
if (Cache["FileVersioner:" + Path] != null) | |
{ | |
var cachedInfo = Cache["FileVersioner:" + Path] as FileVersionInfo; | |
if (lastModified == cachedInfo.LastModified) | |
{ | |
return cachedInfo.Checksum; | |
} | |
Debug.WriteLine("Cached Checksum is invalid for {0}", Path, 0); | |
Cache.Remove("FileVersioner:" + Path); | |
} | |
Debug.WriteLine("Caclulating Checksum for {0}", Path, 0); | |
using (var stream = File.OpenRead(physicalFile)) | |
{ | |
var sha = new SHA1Managed(); | |
var checksum = sha.ComputeHash(stream); | |
var fileChecksum = BitConverter.ToString(checksum).Replace("-", String.Empty); | |
Cache.Insert("FileVersioner:" + Path, new FileVersionInfo { Checksum = fileChecksum, LastModified = lastModified }); | |
return fileChecksum; | |
} | |
} | |
private class FileVersionInfo | |
{ | |
public string Checksum { get; set; } | |
public DateTime LastModified { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment