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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube