Practical .NET

Turn Off Caching at the Browser

You can force users to always get the most recent version of your ASP.NET Web page by using the last tool you'd expect to handle this: The OutputCache attribute.

Occasionally, I'm asked how to ensure that a user will always get the latest version of a page even if, for example, the user returns to the page by pressing their browser's Back button. Normally, of course, the browser caches any page that it sees and, when the user returns to the page through the Back button, the browser displays the cached page rather than going back to the browser. As a result, the user is saved a trip to the server but sees what's now an older version of the page.

That doesn't always work out for the best, as you probably know from the number of times you've refreshed a page or cleared your browser's cache to get the latest version of the page.

Now, for a non sequitur: The OutputCache attribute is a wonderful thing. Adding the attribute to an Action method in ASP.NET MVC causes ASP.NET to skip over the method when it's called for the second time (there's also a variation on it that you can use in ASP.NET Web Forms). Instead of rerunning the Action method, ASP.NET just returns the HTML that was generated on the previous request for the page, reducing demand on your server and improving user response time. The reason that I bring up the OutputCache attribute is because it's also what you should use to turn off client-side caching. First, the OutputCache has a NoStore property that, when set to True, causes ASP.NET to add no-store to your page's cache-control in the page's headers. You can also set the attribute's Location attribute to System.Web.UI.OutputCacheLocation.None, which adds no-cache to your page's cache-control. Those options should (notice the keyword: should) cause everything from the server down to the browser to refuse to cache the page.

Using OutputCache for this seems perverse to me because, of course, I normally use the OutputCache to skip regenerating the View at the server. In fact, if you blindly apply these options you may successfully turn off caching downstream while continuing to return the same page from the server because ASP.NET will skip rerunning the method. The user will get the worst of results: the same page as before but had to go all the way back to the server to get it. To avoid that problem, you should set the attribute's Duration property to 0. This not only effectively turns off OutputCache at the server, it also adds max-age=0 to your cache-control.

Putting all that together, the right settings to do everything possible to make sure that your page is never cached anywhere is:

<OutputCache(Duration := 0, NoStore := True, Location := System.Web.UI.OutputCacheLocation.None)> 
Public Function Index As ActionResult

In other words, with OutputCache's Duration set to 0, all things are possible.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

Subscribe on YouTube