Class Act

Back to the shelf

Javascript · Snippet · ASP.net

No Cache Server and Cliet

Code to tell both the server and the browser not to cache a page, so users always get the newest version.

Example

How to use No Cache Server and Cliet:
Load the script on the page, then call it from the event or function that needs it.

Code

//server
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); // Expire immediately
Response.Cache.SetNoStore();
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

<-- Client -->
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

//asp.net fast way to cache bust
<script src="../Scripts/Tools.js?v=<%= DateTime.Now.Ticks %>"></script>

//another way of doing it, when you deploy
<script src="../Scripts/Tools.js?v=<%= System.Reflection.Assembly.GetExecutingAssembly().GetName().Version %>"></script>