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?
To ensure that JQuery isn't caching the results, on your ajax methods, put the following:
Or to prevent caching in MVC, we created our own attribute, you could do the same. Here's our code:
Then just decorate your controller with
[NoCache]
. OR to do it for all you could just put the attribute on the class of the base class that you inherit your controllers from (if you have one) like we have here:You can also decorate some of the actions with this attribute if you need them to be non-cacheable, instead of decorating the whole controller.
If your class or action didn't have
NoCache
when it was rendered in your browser and you want to check it's working, remember that after compiling the changes you need to do a "hard refresh" (Ctrl+F5) in your browser. Until you do so, your browser will keep the old cached version, and won't refresh it with a "normal refresh" (F5).Correct attribute value for Asp.Net MVC Core to prevent browser caching (including Internet Explorer 11) is:
as described in Microsoft documentation:
Response caching in ASP.NET Core - NoStore and Location.None