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

时间:2022-09-11 14:59:01

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 that way?

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

5 个解决方案

#1


6  

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

您可以尝试使用HttpResponse.Cache属性,如果这将有所帮助:

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:

或者可以使用HttpResponse.CacheControl完全阻止页面的缓存,但是它已被弃用而支持上面的Cache属性:

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 

#2


6  

As far as I know (or at least have read) is its best to try not to work in response to user events, but rather think "in the page"..

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

Architect your application so it doesn't care if the back button is pushed.. It will just deal with it.. This may mean a little extra work from a development point of view, but overall will make the application a lot more robust..

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

I.e if step 3 performs some data chages, then the user clicks back (to step 2) and clicks next again, then the application checks to see if the changes have been made.. Or ideally, it doesnt make any hard changes until the user clicks "OK" at the end.. This way, all the changes are stored and you can repopulate the form based on previously entered values on load, each and every time..

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

I hope that makes sense :)

我希望这是有道理的 :)

#3


2  

RFC 2616 §13.13 says that History and Cache are different things. There should be absolutely no way for cache to affect Back button.

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

If any combination of HTTP headers affects Back button, it's a bug in the browser …with one exception.

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

In HTTPS browsers interpret Cache-control: must-revalidate as request to refresh pages when Back button is used (Mozilla calls it "silly bank mode"). This isn't supported in plain HTTP.

在HTTPS浏览器中解释Cache-control:当使用Back按钮时,必须重新验证作为刷新页面的请求(Mozilla称之为“愚蠢的银行模式”)。普通HTTP不支持此功能。

#4


0  

The best way to deal with it is to probably put a no-cache directive in your ASP.NET pages (or a master page if you're using one). I don't think there's a way to deal with this directly in your ASP.NET code (since the cache decision is happening on the client).

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

As for MVC, don't know how you would accomplish that (assuming it's different from Web Forms-based ASP.NET); I haven't used it.

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

#5


0  

The following code worked for me in IE9+, FF21 and Latest Chrome:

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

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

You can place this in Page_Load() event handler in the MasterPage so that every page in your app requires a round-trip to the server when pressing the back button.

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

#1


6  

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

您可以尝试使用HttpResponse.Cache属性,如果这将有所帮助:

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:

或者可以使用HttpResponse.CacheControl完全阻止页面的缓存,但是它已被弃用而支持上面的Cache属性:

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 

#2


6  

As far as I know (or at least have read) is its best to try not to work in response to user events, but rather think "in the page"..

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

Architect your application so it doesn't care if the back button is pushed.. It will just deal with it.. This may mean a little extra work from a development point of view, but overall will make the application a lot more robust..

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

I.e if step 3 performs some data chages, then the user clicks back (to step 2) and clicks next again, then the application checks to see if the changes have been made.. Or ideally, it doesnt make any hard changes until the user clicks "OK" at the end.. This way, all the changes are stored and you can repopulate the form based on previously entered values on load, each and every time..

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

I hope that makes sense :)

我希望这是有道理的 :)

#3


2  

RFC 2616 §13.13 says that History and Cache are different things. There should be absolutely no way for cache to affect Back button.

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

If any combination of HTTP headers affects Back button, it's a bug in the browser …with one exception.

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

In HTTPS browsers interpret Cache-control: must-revalidate as request to refresh pages when Back button is used (Mozilla calls it "silly bank mode"). This isn't supported in plain HTTP.

在HTTPS浏览器中解释Cache-control:当使用Back按钮时,必须重新验证作为刷新页面的请求(Mozilla称之为“愚蠢的银行模式”)。普通HTTP不支持此功能。

#4


0  

The best way to deal with it is to probably put a no-cache directive in your ASP.NET pages (or a master page if you're using one). I don't think there's a way to deal with this directly in your ASP.NET code (since the cache decision is happening on the client).

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

As for MVC, don't know how you would accomplish that (assuming it's different from Web Forms-based ASP.NET); I haven't used it.

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

#5


0  

The following code worked for me in IE9+, FF21 and Latest Chrome:

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

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

You can place this in Page_Load() event handler in the MasterPage so that every page in your app requires a round-trip to the server when pressing the back button.

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