asp.net-mvc ASP.NET MVC 中何时调用 Session_End()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2373209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is Session_End() called in ASP.NET MVC?
提问by Alex B
I have configured my Web.Configfile as follow in a ASP.NET MVC 2 project:
我Web.Config在 ASP.NET MVC 2 项目中按如下方式配置了我的文件:
<sessionState mode="InProc" timeout="1"/>
And added the following in Global.asax.cs:
并添加了以下内容Global.asax.cs:
protected void Session_End(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Session_End");
}
protected void Session_Start(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Session_Start");
}
Session_Start()is called when a new user goes on the website. I would have expected Session_End()to be called after 1 minute of idle time, but it's not the case. Am I missing something?
Session_Start()当新用户访问网站时调用。我本来希望Session_End()在空闲时间 1 分钟后会被调用,但事实并非如此。我错过了什么吗?
采纳答案by Henk Holterman
Be patient. The event should be called, but not necessarily right after the timeout.
要有耐心。应该调用该事件,但不一定在超时之后立即调用。
You could try from a Browser: Start a session,wait > 1 minute, do a Postback somehow
您可以从浏览器尝试:开始会话,等待 > 1 分钟,以某种方式进行回发
This should help to verify that the Timeout works and I think you will also see the SessionEnd happening at that time. Otherwise, just wait and start some other sessions. The system will come around o calling it sometime.
这应该有助于验证 Timeout 是否有效,我认为您还会看到 SessionEnd 正在发生。否则,只需等待并开始一些其他会话。系统会在某个时候调用它。
回答by Tamás Varga
Remember this:
记住这一点:
If you don't save anything into the session, the session_end will not fire. If you're saving data in the session in the first request, and calling abandon in the same request the session_end will also not fired.
Hope this helps!
T
如果您不将任何内容保存到会话中,则 session_end 将不会触发。如果您在第一个请求的会话中保存数据,并在同一请求中调用放弃,则 session_end 也不会触发。
希望这可以帮助!
吨
btw: ASP.NET Session_End event not firing
顺便说一句: ASP.NET Session_End 事件未触发
回答by Simon_Weaver
Tip for testing:Use Session.Abandonso you don't have to mess with your actual web.config value. Just don't set a session value and call Session.Abandonduring the same request or it won't get stored.
测试提示:使用,Session.Abandon这样您就不必弄乱您的实际 web.config 值。只是不要Session.Abandon在同一请求期间设置会话值和调用,否则它不会被存储。

