代码之家  ›  专栏  ›  技术社区  ›  TheEmirOfGroofunkistan

处理缓存和浏览器后退按钮的最佳方法是什么?

  •  8
  • TheEmirOfGroofunkistan  · 技术社区  · 18 年前

    处理用户返回asp.net应用程序中包含缓存项的页面的最佳方法是什么?是否有一种捕获后退按钮(事件?)并以这种方式处理缓存的好方法?

    5 回复  |  直到 18 年前
        1
  •  7
  •   travis    18 年前

    HttpResponse.Cache property 如果这有帮助的话:

    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"])
    {
       //...
    }
    

    或者可能会完全阻止页面的缓存 HttpResponse.CacheControl ,但它已被弃用,取而代之的是上面的缓存属性:

    Response.CacheControl = "No-Cache";
    

    go nuts 而且都是手工完成的:

    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 
    
        2
  •  6
  •   Rob Cooper    18 年前

    据我所知(或者至少已经读过),最好不要对用户事件做出响应,而是“在页面中”思考。。

    也就是说,如果步骤3执行一些数据更改,那么用户单击返回(到步骤2)并再次单击下一步,然后应用程序检查是否进行了更改。。或者理想情况下,它不会产生任何影响 坚固的

    我希望这是有道理的:)

        3
  •  2
  •   Kornel    17 年前

    RFC 2616 §13.13 历史和缓存是不同的东西 . 缓存绝对不能影响“后退”按钮。

    如果HTTP头的任何组合都会影响Back按钮,那就是浏览器中的一个bug,只有一个例外。

    在HTTP中 s 浏览器解释 Cache-control: must-revalidate 当使用“后退”按钮时,作为刷新页面的请求(Mozilla称之为“愚蠢的银行模式”)。这在普通HTTP中不受支持。

        4
  •  0
  •   TheSmurf    18 年前

    处理此问题的最佳方法可能是在ASP.NET页面(或母版页,如果您正在使用母版页)中放入无缓存指令。我认为在ASP.NET代码中没有直接处理这个问题的方法(因为缓存决策发生在客户端)。

        5
  •  0
  •   Steven de Salas Alexander Bollaert    11 年前

    以下代码在IE9+、FF21和最新的Chrome中对我有效:

    Response.Cache.SetCacheability(HttpCacheability.NoCache | HttpCacheability.Private);
    Response.Cache.AppendCacheExtension("must-revalidate");
    Response.Cache.AppendCacheExtension("max-age=0");
    Response.Cache.SetNoStore();
    

    你可以把这个放在 Page_Load() 母版页中的事件处理程序,以便在按下后退按钮时,应用程序中的每个页面都需要往返到服务器。