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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube