简体   繁体   English

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

[英]How can I prevent the user from navigating back to a previous page?

I have an ASP.NET MVC application, with three views: view1 , view2 , view3 . 我有一个ASP.NET MVC应用程序,具有三个视图: view1view2view3 The logic way for the user to navigate through these is: view1 -> view2 -> view3 . 通过这些用户导航逻辑的方法是: 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. 当用户到达view3 ,即使使用浏览器中的“后退”按钮,我也必须阻止他们加载view2

What is a good, browser-independent means of implementing this? 有什么好的,独立于浏览器的方法来实现此目的?

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. 不要试图欺骗用户,让他想他想做什么,然后再尝试交付一个不完全损坏的页面。

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 您不能在客户端上操纵或禁止基本导航,但是可以在服务器端解决此问题

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. 1)您必须防止浏览器端缓存。 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. 如果您不阻止缓存,则后退按钮将显示view1的缓存。 Since step 2 is done server-side, the server won't have a chance to do anything. 由于步骤2是在服务器端完成的,因此服务器将没有机会做任何事情。

2) You need to, as previous poster's have said, do something on the serverside to prevent the display. 2)您需要像以前的海报所说的那样,在服务器端执行某些操作以防止显示。 There are two ways to do this (despite my really bad pseudo code). 有两种方法可以做到这一点(尽管我的伪代码确实很糟糕)。

a) The quick & dirty way is based on the referer. a)快速和肮脏的方法基于引用者。 For example, you'd put the following check on the controller for view2: 例如,您将以下检查放在了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. 如果您使用表单来回传递值,那么当用户返回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). 但是请注意,有些浏览器永远不会传递引荐来源网址,并且上述检查不会有任何好处(并且request.urlrefferer将为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. b)我以前做过这样的事情。 The controller view1/2/3 is essentially a wizard where they're walking through the system. 控制器view1 / 2/3本质上是一个向导,他们在其中浏览系统。 Each controller updates the db row associated with the wizard. 每个控制器更新与向导关联的数据库行。 So, view 2 would do something like: 因此,视图2会执行以下操作:

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

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). 那不在javascript的范围之内,并且不能被禁用(尽管您可以告诉浏览器前进或后退,但是您不能阻止它)。 You would need a server side solution to disallow access to the pages. 您将需要服务器端解决方案来禁止访问页面。

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

You can add a function to view1 and view 2 that fires on page load, if you use jquery something like : 您可以向view1和view 2添加一个在页面加载时触发的函数,如果您使用jquery之类的东西:

$(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. 两种方法都可以满足您的需求,但就用户体验而言,它可能不是最佳解决方案-但是,如果您需要使用它,那么它可能是最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM