我是mvc应用程序的新手,我正在使用MVC 5版本。我现在面临的情况就像是如果我点击浏览器后退按钮停留在我的应用程序内的任何网页内,那么应该显示登录页面而不是显示任何缓存页。
我在global.asax中添加了以下代码来清除缓存。过了一段时间后,如果清除缓存并单击后退按钮,则会显示过期页面。
protected void Application_BeginRequest(){
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
}
如果在aplication中点击了浏览器后退按钮,我的目的是在任何时候显示Login操作方法。请帮帮我们..
答案 0 :(得分:0)
According to me this should never be done. The back button on browser is a client / user action and it has a specific behaviour. You should not try to change something like that.
But its all up to you if you want to do something that would be really bad according to user persceptive. What you can do is use the unload event in JavaScript or jquery. This will not just let you know when back button is pressed but when anytime the page is navigated away.
I had used this when dealing with data entry form to confirm navigation away after user has entered data without saving.
The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.
$( window ).unload(function() { return "Handler for .unload() called."; });
I think now you would be thinking of distinguishing what causes the unload event. But that is not possible, at least not in a cross browser way and without any hacks. You can refer this link for more details about it - Is there a way in javascript to detect if the unload event is caused via a refresh, the back button, or closing the browser?
update (copied from another SO answer):
Since you are doing this for logout-purposes you should probably use a timestamp variable at the server that gets updated with every request (or use a ajax-ping), and logout the user if it hasn't been seen for a specified time.