Disable browser cache for entire ASP.NET website

前端 未结 8 1375
既然无缘
既然无缘 2020-11-22 03:20

I am looking for a method to disable the browser cache for an entire ASP.NET MVC Website

I found the following method:

Response.Cach         


        
相关标签:
8条回答
  • 2020-11-22 03:58

    Create a class that inherits from IActionFilter.

    public class NoCacheAttribute : ActionFilterAttribute
    {  
        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);
        }
    }
    

    Then put attributes where needed...

    [NoCache]
    [HandleError]
    public class AccountController : Controller
    {
        [NoCache]
        [Authorize]
        public ActionResult ChangePassword()
        {
            return View();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:59

    You may want to disable browser caching for all pages rendered by controllers (i.e. HTML pages), but keep caching in place for resources such as scripts, style sheets, and images. If you're using MVC4+ bundling and minification, you'll want to keep the default cache durations for scripts and stylesheets (very long durations, since the cache gets invalidated based on a change to a unique URL, not based on time).

    In MVC4+, to disable browser caching across all controllers, but retain it for anything not served by a controller, add this to FilterConfig.RegisterGlobalFilters:

    filters.Add(new DisableCache());
    

    Define DisableCache as follows:

    class DisableCache : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:05

    You can try below code in Global.asax file.

    protected void Application_BeginRequest()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
        }
    
    0 讨论(0)
  • 2020-11-22 04:12

    I know this answer is not 100% related to the question, but it might help someone.

    If you want to disable the browser cache for the entire ASP.NET MVC Website, but you only want to do this TEMPORARILY, then it is better to disable the cache in your browser.

    Here's a screenshot in Chrome

    0 讨论(0)
  • 2020-11-22 04:12

    I implemented all the previous answers and still had one view that did not work correctly.

    It turned out the name of the view I was having the problem with was named 'Recent'. Apparently this confused the Internet Explorer browser.

    After I changed the view name (in the controller) to a different name (I chose to 'Recent5'), the solutions above started to work.

    0 讨论(0)
  • 2020-11-22 04:14
    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.

    0 讨论(0)
提交回复
热议问题