Disable browser cache for entire ASP.NET website

前端 未结 8 1389
既然无缘
既然无缘 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: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);
        }
    }
    

提交回复
热议问题