Disable browser cache for entire ASP.NET website

前端 未结 8 1391
既然无缘
既然无缘 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 04:17

    UI

    <%@ OutPutCache Location="None"%>
    <%
        Response.Buffer = true;
        Response.Expires = -1;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.CacheControl = "no-cache";
    %>
    

    Background

    Context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.Expires = -1;          
    Response.Cache.SetNoStore();
    
    0 讨论(0)
  • 2020-11-22 04:19

    Instead of rolling your own, simply use what's provided for you.

    As mentioned previously, do not disable caching for everything. For instance, jQuery scripts used heavily in ASP.NET MVC should be cached. Actually ideally you should be using a CDN for those anyway, but my point is some content should be cached.

    What I find works best here rather than sprinkling the [OutputCache] everywhere is to use a class:

    [System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public class NoCacheController  : Controller
    {
    }
    

    All of your controllers you want to disable caching for then inherit from this controller.

    If you need to override the defaults in the NoCacheController class, simply specify the cache settings on your action method and the settings on your Action method will take precedence.

    [HttpGet]
    [OutputCache(NoStore = true, Duration = 60, VaryByParam = "*")]
    public ViewResult Index()
    {
      ...
    }
    
    0 讨论(0)
提交回复
热议问题