Click here to Skip to main content
15,881,715 members
Articles / Web Development / CSS

JavaScript and CSS Caching using CRC32

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
16 Jul 2012MIT5 min read 36.3K   186   13  
A simple method to ensure that new .JS and .CSS files always get downloaded and cached ones never get re-downloaded
using System.IO;
using System.Web;

namespace WebCRC
{
    public static class CRC
    {
        public static string Content(this System.Web.Mvc.UrlHelper Url, string Path, bool CRC)
        {
            if (!CRC)
                return Url.Content(Path);

            string serverPath = HttpContext.Current.Server.MapPath(Path);
            byte[] fileContents = File.ReadAllBytes(serverPath);
            string result = CRC32.Compute(fileContents).ToString("X");
            return Url.Content(Path) + "?crc=" + result;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Web Developer
United States United States
Paul Reid is the CEO (and still part-time programmer) of Product4Me Corporation, a web site company devoted to helping consumers get the individualized best fit for home theater (and other) products.

Comments and Discussions