当我按浏览器的“后退”按钮时会发生什么?

发布于 2024-08-19 19:30:01 字数 269 浏览 7 评论 0原文

考虑以下场景:

  1. 我访问了使用 ASP.NET 构建的网站的页面。该页面是一个包含 ASP.NET 服务器控件的简单 aspx 页面。

  2. 我单击了一个链接,该链接将我带到同一网站上的其他页面。

    我单击
  3. 我单击了浏览器的BACK按钮。

问题:页面生命周期会发生什么?是所有事件都发生还是浏览器只显示页面的缓存版本而不发出任何请求?

Consider the scenario:

  1. I visited a page of a website built using ASP.NET. The page is a simple aspx page containing ASP.NET server controls.

  2. I clicked on a link which takes me to some other page on the same website.

  3. I clicked the BACK button of the browser.

QUESTION: What happens in terms of page life cycle? Does all the events occur or the browser just displays the cached version of the page without making any requests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若水般的淡然安静女子 2024-08-26 19:30:01

我认为最好的答案是:这取决于浏览器,尤其是在发布/回发之后。

旧版浏览器过去会弹出一个确认对话框,大意是“该页面包含将重新提交的 POST 数据”,您可以继续(重新提交)或取消。由于 ASP.NET WebForms 中发生的所有事情都是 FORM 元素的一部分(ViewState、事件等),这将导致整个生命周期重复。

当然,这会导致重复提交的麻烦不断,因此许多网站不得不想出解决重复问题的方法,而如今大多数浏览器只是从缓存中获取页面。

...那是除非您覆盖缓存控制标头并强制浏览器不将页面存储在缓存中。显然,在这种情况下,无法从缓存中检索它,因此通常最终会重新提交。但是,这又取决于浏览器 - 例如,某些浏览器不允许通过 SSL 重新提交,因此如果这是正在使用的协议,那么用户只会看到一条消息,指出该页面已过期/无法访问显示。

想一想,可能更好的答案是:作为网站设计者,您确实不能依赖于单击“后退”按钮时用户浏览器的任何特定行为。如果重复提交可能会产生负面影响(例如向信用卡收取两次费用),那么您需要采取适当的措施来防止这种情况发生。无论如何,这是一个很好的做法,因为用户完全有可能意外地双击“提交”按钮。

I think the best answer is: It depends on the browser, especially after a post/postback.

Older browsers used to pop up a confirmation dialog to the effect of "the page contains POST data which will be resubmitted", and you could either proceed (resubmit) or cancel out. Since everything that happens in ASP.NET WebForms is part of the FORM element (ViewState, events, etc.), this would cause the entire lifecycle to be repeated.

Of course, this caused no end of trouble with duplicate submissions, so many sites had to come up with workarounds for the dupe problem, and today most browsers just fetch the page from cache instead.

...That is unless you override the cache-control headers and force the browser not to store the page in cache. Obviously, in that case, it can't be retrieved from cache, so it will usually end up being resubmitted. But, again, it depends on the browser - for example, some browsers won't allow the resubmission over SSL, so if that's the protocol in use then the user will just see a message saying that the page has expired / can't be shown.

Come to think of it, probably an even better answer is: As a site designer, you really can't depend on any specific behaviour from the user's browser when the Back button is clicked. If a duplicate submission could have negative side-effects (such as charging a credit card twice), then you need to take adequate measures to prevent that from happening. It's good practice anyway as it's entirely possible for a user to simply double-click the "submit" button by accident.

梦魇绽荼蘼 2024-08-26 19:30:01

我们甚至尝试过

Response.ExpiresAbsolute = DateTime.Parse("1/1/1980");
Response.AddHeader("cache-control", "no-store, must-revalidate, private");
Response.AddHeader("Pragma", "no-cache");

解决此类问题

we have even tried

Response.ExpiresAbsolute = DateTime.Parse("1/1/1980");
Response.AddHeader("cache-control", "no-store, must-revalidate, private");
Response.AddHeader("Pragma", "no-cache");

to resolve this kind of problem

抠脚大汉 2024-08-26 19:30:01

该页面将从缓存中显示。

The page would be displayed from Cache.

月寒剑心 2024-08-26 19:30:01

通常所有事件都应该发生,但如果您有超级浏览器,则可能会显示缓存的页面
您可以在页面加载中放置一个断点并查看是否会发生

usually all the events should occur, but if you have an uber browser than it could happen to display a cached page
you can just put a breakpoint in your Page Load and see if it's going to occur

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文