I'm looking for ways to disable browser caching for the entire ASP. NET MVC Site
I found the following methods,
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Also meta tag method (it won't work for me because some MVC operations send part Html/json via Ajax, headless, meta tags)
<meta. http-equiv="PRAGMA" content="NO-CACHE">
But I'm looking for a simple way to disable browser caching for the entire site.
Workaround 1:
HttpContext.Current.Response.Cache.SetExpires(Datetime.UtcNow.AddDays(-1))
HttpContext.Current.Response.Cache.SetValidUntilExpires(False)
HttpContext.Current.Response.Cache.Setrevalidation (httpcacherevalidation. Allcaches
httpcontext. Current. Response. Cache. Setcacheability (httpcacheability. Nocache
httpcontext. Current. Response. Cache. Setnostore ()
All requests get routed through Default.aspx for the first time-so assume you can just pop out there in the code later.
Workaround 2:
Create a class that inherits from Iactionfilter.
Public Class NoCache : ActionFilterAttribute
{
Public Override void Onresultexecuting(ResultExecutingContextFiltercontext)
{
Filtercontext.HttpContext.Response.Cache.SetExpires(Datetime.UtcNow.AddDays(-1));
Filtercontext.HttpContext.Response.Cache.SetValidUntilExpires(False);
Filtercontext.HttpContext.Response.Cache.Setrevalidation(Httpcacherevalidation.Allcaches);
Filtercontext.HttpContext.Response.cache. Setcacheability (httpcacheability. Nocache
Filtercontext. Httpcontext. Response. Cache. Setnostore
base. Onresultexecutingfiltercontext }
}
Then put the attributes where needed ...
[NoCache]
[HandleError]
Public Class AccountController : controller
{< Span class= "PLN" >
[nocache
[authorize
public actionresult< Span class= "PLN" > changepassword ()
{
return view (); }
}
. NET MVC disables browser caching