What's the best way to deal with cache and the browser back button?

前端 未结 5 1710
心在旅途
心在旅途 2021-02-06 03:24

What\'s the best way to handle a user going back to a page that had cached items in an asp.net app? Is there a good way to capture the back button (event?) and handle the cache

5条回答
  •  一向
    一向 (楼主)
    2021-02-06 04:23

    You can try using the HttpResponse.Cache property if that would help:

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(false);
    Response.Cache.VaryByParams["Category"] = true;
    
    if (Response.Cache.VaryByParams["Category"])
    {
       //...
    }
    

    Or could could block caching of the page altogether with HttpResponse.CacheControl, but its been deprecated in favor of the Cache property above:

    Response.CacheControl = "No-Cache";
    

    Edit: OR you could really go nuts and do it all by hand:

    Response.ClearHeaders();
    Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
    Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
    Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
    Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
    Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1 
    Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1 
    Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1 
    Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1 
    

提交回复
热议问题