In addition to the accepted answer, to support VaryByParam parameters:
[OutputCache (Duration=3600, VaryByParam="param1;param2", Location = OutputCacheLocation.Server)] public string AjaxHtmlOutputMethod(string param1, string param2) { object routeValues = new { param1 = param1, param2 = param2 }; string url = Url.Action("AjaxHtmlOutputMethod", "Controller", routeValues); Response.RemoveOutputCacheItem(url); }
However, Egor's answer is much better, because it supports all the values ββof OutputCacheLocation:
[OutputCache (Duration=3600, VaryByParam="param1;param2")] public string AjaxHtmlOutputMethod(string param1, string param2) { if (error) { Response.Cache.SetNoStore(); Response.Cache.SetNoServerCaching(); } }
When SetNoStore () and SetNoServerCaching () , they prevent caching of the current request. Further requests will be cached if functions are also not called for these requests.
This is the ideal solution for handling error situations - when you usually want to cache responses, but not if they contain error messages.
user1023602 May 04 '16 at 10:01 2016-05-04 10:01
source share