0

I want to prevent browsers from caching HTML pages which they unfortunately do by default.

I understand the headers I need to set. But where in my ASP.NET MVC app is the appropriate place to set them?

The controllers do not seem right since that would be repetitive code and controllers can send non-HTML responses as well (e.g. JSON).

BeginRequest appears too broad since that would affect resources (and anything else I might not realize).

The suggested duplicate does not solve this issue: I only want to affect HTML pages (not the entire site) and the duplicate does not say where to add this code (it says "add where needed").

boot4life
  • 4,292
  • 5
  • 20
  • 40
  • 2
    In MVC, the most appropriate place to put [cross-cutting concerns](https://msdn.microsoft.com/en-us/library/ee658105.aspx) are in [filters](https://msdn.microsoft.com/en-us/library/gg416513(vs.98).aspx). For your simple example, you could just subclass `ActionFilterAttribute` and override `OnActionExecuting` to write the header, and then register the filter globally in `FilterConfig.cs`. – NightOwl888 Apr 17 '17 at 13:59
  • @NightOwl888 where would I apply that attribute? Is there no way around applying it *everywhere*? – boot4life Apr 17 '17 at 14:36
  • Possible duplicate of [Disable browser cache for entire ASP.NET website](http://stackoverflow.com/questions/1160105/disable-browser-cache-for-entire-asp-net-website) – Leonid Vasilev Apr 17 '17 at 15:10
  • In your `FilterConfig.cs` file, you only need to add your filter once and then it is applied on every request. There is then no need to add it everywhere. – NightOwl888 Apr 17 '17 at 16:41

0 Answers0