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

时间:2008-08-22 18:19:10

标签: asp.net caching back-button

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

5 个答案:

答案 0 :(得分:7)

如果有帮助,您可以尝试使用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完全阻止页面的缓存,但是它已被弃用,而不是上面的Cache属性:

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 

答案 1 :(得分:6)

据我所知(或至少已阅读),最好尽量不要回应用户事件,而是“在页面中”思考..

构建你的应用程序,因此它不关心是否按下了后退按钮..它只是处理它...从开发的角度来看这可能意味着一些额外的工作,但整体上会使应用程序很多更健壮..

即如果第3步执行某些数据更改,则用户单击返回(到第2步)并再次单击下一步,然后应用程序检查是否已进行更改..或者理想情况下,它不会使任何 hard 更改,直到用户点击“OK”结束。这样,所有更改都会被存储,您可以根据以前输入的值在每次加载时重新填充表单。

我希望这是有道理的:)

答案 2 :(得分:2)

RFC 2616 §13.13历史记录和缓存是不同的东西。缓存应该绝对没有办法影响后退按钮。

如果HTTP标头的任何组合影响“后退”按钮,则它是浏览器中的错误...但有一个例外。

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

答案 3 :(得分:0)

处理它的最好方法是在你的ASP.NET页面中放置一个no-cache指令(如果你使用的话,可以放入一个母版页)。我认为没有办法直接在ASP.NET代码中处理这个问题(因为缓存决策是在客户端上发生的)。

对于MVC,不知道如何实现这一点(假设它与基于Web Forms的ASP.NET不同);我没用过它。

答案 4 :(得分:0)

以下代码适用于IE9 +,FF21和最新Chrome:

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

您可以将其放在MasterPage中的Page_Load()事件处理程序中,这样当您按下后退按钮时,应用中的每个页面都需要往返服务器。