Global.asax的Application_End事件中的访问会话变量 [英] Access session variable in Global.asax's Application_End event

查看:68
本文介绍了Global.asax的Application_End事件中的访问会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当程序执行开始Application_Start时,Session_start将从global.asax中调用这些方法.

像这样在程序执行停止时调用哪个方法?以及如何在其中访问会话变量?


我的主要问题是我想在浏览器关闭时清除会话变量.为此,我正在上面尝试.

如果您知道其他更好的解决方案,请提出建议.


在此先感谢.

hi ,

As when program execution starts Application_Start, Session_start these methods from global.asax get called.

Such like this which method is called when program execution stops? and how can i access session variable in this ?


My main problem is i want to clear session variables on browser close. For this i am trying above.

If any else better solution you knows please suggest me.


Thanks in advance.

推荐答案

Session属性提供对HttpSessionState类的属性和方法的编程访问.因为ASP.NET页包含对System.Web命名空间的默认引用(包含HttpContext类),所以您可以在.aspx页上引用HttpContext的成员,而无需对HttpContext进行完全限定的类引用.例如,您可以仅使用Session("SessionVariable1")来获取或设置会话状态变量SessionVariable1的值.但是,类文件不会继承System.web命名空间.所以我们需要如下访问,
The Session property provides programmatic access to the properties and methods of the HttpSessionState class. Because, ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. For example, you can use just Session("SessionVariable1") to get or set the value of the session state variable SessionVariable1. However, class file will not inherit System.web namespace. So we need access like below,
HttpContext.Current.Session("Session_Name")


Application_Start和_End事件与整个Web应用程序实例有关,即从IIS加载或卸载它. 无关,它与特定用户或会话结束无关.在这些事件中引用当前会话"是没有意义的(请考虑在静态处理程序中引用实例属性).

此外,在通常情况下,无法检测到用户何时关闭浏览器.

我认为您正在尝试解决一个不存在的问题:ASP.net已经为您清除了过期(或显式终止)的会话.
The Application_Start and _End events are related to your web application instance as a whole, i.e. when it is loaded or unloaded from IIS. It is not related to a particular user or session ending. It does not make sense to refer to the ''current session'' in these events (think referring to instance properties in a static handler).

Furthermore, it is impossible to detect when the user closes his browser, in the general case.

I think you are trying to solve a problem that doesn''t exist: ASP.net cleans up expired (or explicitly terminated) sessions for you already.


这篇关于Global.asax的Application_End事件中的访问会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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