如何防止用户导航回上一页?

发布于 2024-07-25 15:08:06 字数 302 浏览 4 评论 0原文

我有一个 ASP.NET MVC 应用程序,具有三个视图:view1view2view3。 用户浏览这些内容的逻辑方式是:view1 -> view2 -> 视图3

当用户到达 view3 时,我必须阻止他们加载 view2,即使使用浏览器中的“后退”按钮也是如此。

有什么好的、独立于浏览器的方法来实现这一点?

I have an ASP.NET MVC application, with three views: view1, view2, view3. The logic way for the user to navigate through these is: view1 -> view2 -> view3.

When the user reaches view3, then I must prevent them from loading view2, even by using the "Back" button in their browser.

What is a good, browser-independent means of implementing this?

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

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

发布评论

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

评论(6

花落人断肠 2024-08-01 15:08:06

在大多数应用程序中,您必须应对浏览器的返回功能。 用户已经习惯了它并且他想要使用它,并且他或多或少会讨厌那些在来回时试图欺骗他们的页面。

不要试图欺骗用户思考他想要做什么,然后尝试提供一个未完全损坏的页面。

In most of the applications you have to cope with the back ability from the browser. The user is used to it and he wants to use it and he more or less will hate pages that try to trick them when going back and forward.

Don't try to fool you user think about what he wanted to do and then try do deliver a not completely broken page.

笑梦风尘 2024-08-01 15:08:06

在应用程序中的页面加载时添加引用页面检查,然后显示页面或将用户重定向回使用的视图。 您无法在客户端上操作或禁止基本导航,但您可以在服务器端解决此问题

Add a check of referrer page on page load in your application and then show a page or redirect user back to used view. You cannot manipulate or disallow basic navigation on client, but you can solve this problem server-side

屋檐 2024-08-01 15:08:06

我无法对之前的帖子发表评论,但请注意,某些浏览器不会传递引荐来源网址,因此早期的解决方案会中断(实际上会引发异常)。

此操作有两个步骤:

1)你必须阻止浏览器端缓存。 如果您有一个用户走过的三步流程并且是动态的,那么您可能已经在这样做了。 如果不阻止缓存,后退按钮会显示view1的缓存。 由于步骤 2 是在服务器端完成的,因此服务器将没有机会执行任何操作。

2)正如之前的海报所说,您需要在服务器端做一些事情来阻止显示。 有两种方法可以做到这一点(尽管我的伪代码非常糟糕)。

a) 快速& dirty way是基于referer的。 例如,您可以对 view2 的控制器进行以下检查:

if (request.urlreferrer.absolutepath == "controllerview1")
{ //good }
else
{ //bad }

另外,在“坏”的情况下,您必须考虑该怎么做。 如果您使用表单来回传递值,那么当用户返回 view2 时您会突然迷失方向。

但请注意,某些浏览器永远不会通过引荐来源网址,并且上述检查不会有任何好处(并且 request.urlrefferer 将为空)。(我相信这通常是由于防火墙造成的。 )在这种情况下你必须这样做:

b)我以前做过类似的事情。 控制器 view1/2/3 本质上是一个向导,他们在其中遍历系统。 每个控制器都会更新与向导关联的数据库行。 因此,视图 2 会执行以下操作:

if (dbrow.last_saved_page_num == 1)
{ // good }
else
{ // bad
  redirect("view" + dbrow.last_saved_page_num + 1);
}

I can't comment on the earlier posts, but note that some browsers don't pass referrers, and thus the earlier solution would break (throw an exception, actually).

There are two steps to this:

1) You have to prevent browser-side caching. If you've got a three step process that the user walks through and it's dynamic, you're probably already doing this. If you don't prevent caching, the back button will show the cache of view1. Since step 2 is done server-side, the server won't have a chance to do anything.

2) You need to, as previous poster's have said, do something on the serverside to prevent the display. There are two ways to do this (despite my really bad pseudo code).

a) The quick & dirty way is based on the referer. For example, you'd put the following check on the controller for view2:

if (request.urlreferrer.absolutepath == "controllerview1")
{ //good }
else
{ //bad }

Also, in the case of "bad", you'll have to consider what to do. If you're using forms to pass values back and forth, you've suddenly lost when the user goes back to view2.

Note, though, that some browsers don't ever pass referrers and the above check won't do any good (and request.urlrefferer will be null). (I believe this is generally due to firewalls.) In which case you'd have to do:

b) I've done something like this before. The controller view1/2/3 is essentially a wizard where they're walking through the system. Each controller updates the db row associated with the wizard. So, view 2 would do something like:

if (dbrow.last_saved_page_num == 1)
{ // good }
else
{ // bad
  redirect("view" + dbrow.last_saved_page_num + 1);
}
兮子 2024-08-01 15:08:06

这超出了 javascript 的范围,并且无法禁用(尽管您可以告诉浏览器前进或后退,但您无法阻止它)。 您需要一个服务器端解决方案来禁止访问页面。

That is outside of the scope of javascript, and cannot be disabled (though you can tell the browser to go forward or back, you cannot prevent it). You would need a server side solution to disallow access to the pages.

无人问我粥可暖 2024-08-01 15:08:06

没有 JavaScript 解决方案,它必须在服务器端实现。

There is no JavaScript solution, it would have to be implemented server side.

自找没趣 2024-08-01 15:08:06

您可以向 view1 和 view 2 添加一个在页面加载时触发的函数,如果您使用 jquery 之类的东西:

$(document).ready(function(){ window.history.forward(); });

或者您可以这样

<html onload="window.history.forward()">

做用户体验 - 但如果这就是您必须处理的所有问题,那么它可能是最好的解决方案。

You can add a function to view1 and view 2 that fires on page load, if you use jquery something like :

$(document).ready(function(){ window.history.forward(); });

or you could just just do this

<html onload="window.history.forward()">

Either way will do pretty much what you want, but maybe not be the best solution in terms of user experience - but if its all you have to work with then it might be the best solution.

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