You’ve seen this problem before- you deploy a new version of your website but the style is off and you’re getting weird javascript errors. You know the issue: Firefox or IE is caching and old version of the css/js file and it’s screwing up the web app. The user needs to clear the cache so the latest version is pulled. The solution: versionstamp your include files!
Take a lesson from Rails and create a helper which appends a stamp to your include files (and takes care of the other required markup). It’s simple- embed the following code in your views:
[code lang='csharp'] <%=SiteHelper.JsUrl("global.js") %> [/code]
will render
[code language='html'] [/code]
The browser will invalidate the cache because of the new query string and you’ll be problem free. Version stamps are better than timestamps because the version will only change if you redeploy your site.
Here’s the code, which is based on the AppHelper for Rob Conery’s Storefront MVC:
[code lang='csharp'] using System.Reflection; using System.Web.Mvc;
namespace ViewSample { public static class SiteHelper { private static readonly string _assemblyRevision = Assembly.GetExecutingAssembly().GetName().Version.Build.ToString() + Assembly.GetExecutingAssembly().GetName().Version.Revision.ToString();
///
///
string result = string.Format("", ContentRoot, cssFile, _assemblyRevision);
return result;
}
///
} }
[/code]
Related Posts:












Pingback: ASP.NET MVC Archived Blog Posts, Page 1