views:

4449

answers:

2

I am looking for method to disable Browser Cache for entire ASP.Net MVC Website

I found following method,

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();

and also meta tag method, ( It wont work for me , since some MVC Actions send partial html/json through ajax, without head,meta tag )

<meta http-equiv="PRAGMA" content="NO-CACHE">

But i am looking for simple method, to disable browser cache for entire website.

+4  A: 
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 first - so assuming you can just pop in code behind there.

Paul
I would put it into Global.asax.cs in Application_BeginRequest(). I don't trust this default.aspx thing... Another question: does this have precedence over [OutputCache] attributes?
chris166
I like the idea of simply creating a Global Action Filter an putting this stuff in that way. Negates the need to worry about Default.aspx and Global.asax.
Nissan Fan
Putting this in Application_BeingRequest can cause some issues. If your images get routed through the .net runtime (which can happen if you're using wildcard mapping for nice urls) then no images will be cached on the browser. This can REALLY slow down your page load times as each page request will re-download all images.
herbrandson
+51  A: 

Create a class that inherits from IActionFilter.

public class NoCache : ActionFilterAttribute, IActionFilter
{  
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        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.OnResultExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);
    }
}

Then put attributes where needed...

[NoCache]
[HandleError]
public class AccountController : Controller
{
    [NoCache]
    [Authorize()]
    public ActionResult ChangePassword()
    {
        return View();
    }
}
JKG
Rather than HttpContext.Current.Response, you should probably use filterContext.HttpContext.Response since HttpContext.Current returns the pre-MVC HttpContext object and the filterContext.HttpContext returns the post-MVC HttpContextBase. It increases testability and consistency.
mkedobbs
Good catch and lazy on my part, I've made the changes.
JKG
IActionFilter is already implemented on the ActionFilterAttribute, so you don't need to repeat it.
Andrew Davey
In current versions of ASP.NET MVC you can simply use OutputCacheAttribute to prevent caching:[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
Ashley Tate
Isn't the OutputCacheAttribute only for the server caching? But Palani's question is about browser caching..
jhexp
@jhexp, OutputCache and setting Response.Cache is (almost) the same thing, except some minor limitations on OutputCache.
Bill Yang
@bill-yang Thats true, good point. But there is still a difference between browser caching (controlled by the meta tags or SetExpires() etc) and the ASP.NET Output caching (OutputcacheAttribute and Response.Cache()). For example you may want to completely disable browser caching, but use output caching (to achieve "donut" caching). Its bit offtopic now, but the original question was only about browser cache, eg setting the right metatags and http response headers.
jhexp
I'd like to point out that I just spent several days using every "put this in your code to stop caching" solution under the sun for ASP.NET MVC, including the accepted answer to this question, to no avail. This answer - the attribute - worked. +1M Rep if I could...
Schnapple