关闭网页时如何丢弃会话变量? [英] How does one discard a session variable while closing Web Page?

查看:87
本文介绍了关闭网页时如何丢弃会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发网页时,我们正在遵循一个过程,就是将页面绑定到一个或多个会话变量,这些会话变量仅用于该页面,以保存当前的处理对象,因此在关闭页面时无需为他们.

We are following a procedure in our work while developing a web page, is to bind page to one or more session variables, these session variables are used only for that page, to hold current processing objects, so while closing page no need for them.

在关闭页面时如何丢弃这些会话变量? 关于该技术或如何解决该问题的任何建议?

How could I discard these session variables while closing page? Any suggestions regarding that technique or how to solve that problem?

推荐答案

左/关页面时,不会引发服务器端事件.离开页面时,也不会调用Session_End事件(在其他答案中提到),因为用户可能会导航到同一Web应用程序的其他页面(因此会话将继续存在).

There is no server-side event that is raised when a page is left/closed. Also the Session_End event (mentioned in other answers) is not called when a page is left, since the user might navigate to other pages of the same web application (and therefore the session will continue to exist).

我可以想到3种解决(或解决)此问题的方法:

I can think of 3 possible ways to solve (or work around) this issue:

1 -使用 ViewState 存储具有页面范围的数据.这就是ViewState的用途,除非您有大量数据,否则应该不会有问题.如果您有很多数据,请记住,它将对每个请求进行序列化/反序列化,然后将其发送到客户端/返回到服务器(这可能会导致大量请求,从而降低性能).

1 - use ViewState to store data with page-scope. This is what ViewState is made for, and unless you have a lot of data, it should not be a problem. If you have a lot of data, remember, that it will be serialized/deserialized and sent to the client/back to the server for every request (which may result in large requests and therefore bad performance).

2 -而不是将数据放入会话中,而是将其放入 Cache 中(滑动过期时间很短).在页面上,您可以通过与会话相同的方式访问数据,即data = Cache ["data"],但是您必须做好从缓存中删除数据的准备(必须重新加载数据)例如,如果两个请求之间的时间大于到期时间,则再次从DB中获取.

2 - instead of putting the data into the session, put it into the Cache (with a low sliding expiration timeout). On your page, you can access your data in the same way as from the session, i.e. data = Cache["data"], but you have to be prepared that the data was removed from the Cache (you have to re-load it again from DB for example), if the time between two requests was bigger than the expiration time.

3 -使用客户端(javascript) onUnload 事件,并触发某些操作(例如ajax回调)以删除会话中的数据.但是我认为onUnload事件是不可靠的(在任何情况下都不会触发该事件,例如,当浏览器因崩溃或任务管理器而终止,或者禁用了javascript时).

3 - use the client-side (javascript) onUnload event, and trigger some action (e.g. a ajax callback) to remove the data from the session. But I think the onUnload event is not reliable (it will not be fired in any case, e.g. when the browser is terminated by a crash or with the task manager, or if javascript is disabled).

这篇关于关闭网页时如何丢弃会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆