Skip to main content
 首页 » 编程设计

c#之为什么 "Session_end"不工作

2024年02月20日15jillzhang

global.asax文件中的事件是这样写的:

protected void Session_End(Object sender, EventArgs e) 
{ 
    var x = 5; 
} 

客户端关闭窗口时代码没有到达那里,为什么?

请您参考如下方法:

The code does not reach there when the client close the window, why?

因为当客户端关闭浏览器时,不应调用此事件。此事件(顾名思义)旨在在 session 到期时调用。默认 session 到期超时为 20 分钟,因此将在客户端关闭其浏览器 20 分钟后调用此事件。

还有一个非常重要的陷阱,您应该注意:如果您使用的是进程外 session 状态模式(例如 StateServer 或 SqlServer),将永远不会调用此事件。