I have an ASP.NET MVC 3 application. This application requests records through JQuery. JQuery calls back to a controller action that returns results in JSON format. I have not been able to prove this, but I'm concerned that my data may be getting cached.
I only want the caching to be applied to specific actions, not for all actions.
Is there an attribute that I can put on an action to ensure that the data does not get cached? If not, how do I ensure that the browser gets a new set of records each time, instead of a cached set?
All you need is:
or, if you want to disable it for an entire Controller:
Despite the debate in comments here, this is enough to disable browser caching - this causes ASP.Net to emit response headers that tell the browser the document expires immediately:
You can use the built in cache attribute to prevent caching.
For .net Framework:
[OutputCache(NoStore = true, Duration = 0)]
For .net Core:
[ResponseCache(NoStore = true, Duration = 0)]
Be aware that it is impossible to force the browser to disable caching. The best you can do is provide suggestions that most browsers will honor, usually in the form of headers or meta tags. This decorator attribute will disable server caching and also add this header:
Cache-Control: public, no-store, max-age=0
. It does not add meta tags. If desired, those can be added manually in the view.Additionally, JQuery and other client frameworks will attempt to trick the browser into not using it's cached version of a resource by adding stuff to the url, like a timestamp or GUID. This is effective in making the browser ask for the resource again but doesn't really prevent caching.
On a final note. You should be aware that resources can also be cached in between the server and client. ISP's, proxies, and other network devices also cache resources and they often use internal rules without looking at the actual resource. There isn't much you can do about these. The good news is that they typically cache for shorter time frames, like seconds or minutes.
Output Caching in MVC
In the controller action append to the header the following lines
For MVC6 (DNX), there is no
System.Web.OutputCacheAttribute
Note: when you set
NoStore
Duration parameter is not considered. It is possible to set an initial duration for first registration and override this with custom attributes.But we have
Microsoft.AspNet.Mvc.Filters.ResponseCacheFilter
It is possible to override initial filter with a custom attribute
Here is a use case
Here's the
NoCache
attribute proposed by mattytommo, simplified by using the information from Chris Moschini's answer: