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

comments powered by Disqus


  • Death of the Dev Machine?

    Here's a takeaway from this week's Ignite 2020 event: An advanced Azure cloud portends the death of the traditional, high-powered dev machine packed with computing, memory and storage components.

  • COVID-19 Is Ignite 2020's Elephant in the Room: 'Frankly, It Sucks'

    As in all things of our new reality, there was no escaping the drastic changes in routine caused by the COVID-19 pandemic during Microsoft's big Ignite 2020 developer/IT pro conference, this week shifted to an online-only event after drawing tens of thousands of in-person attendees in years past.

  • Visual Studio 2019 v16.8 Preview Update Adds Codespaces

    To coincide with the Microsoft Ignite 2020 IT pro/developer event, the Visual Studio dev team shipped a new update, Visual Studio 2019 v16.8 Preview 3.1, with the main attraction being support for cloud-hosted Codespaces, now in a limited beta.

  • Speed Lines Graphic

    New for Blazor: Azure Static Web Apps Support

    With Blazor taking the .NET web development world by storm, one of the first announcements during Microsoft's Ignite 2020 developer/IT event was its new support in Azure Static Web Apps.

  • Entity Framework Core 5 RC1 Is Feature Complete, Ready for Production

    The first release candidate for Entity Framework 5 -- Microsoft's object-database mapper for .NET -- has shipped with a go live license, ready for production.

Upcoming Events