ASP.NET MVC:在 Session_End 或之前的事件中访问会话数据
我已经在此处看到了与此类似的问题。我基本上有同样的问题,但有一点小小的不同。
我试图从 Session_End 方法中访问 Session 信息,但当我到达那里时 HttpContext 为空,所以我没有找到任何真正的方法来访问它;就好像在 Session_End 触发时它已经被完全删除了。
相关问题中的解决方案说我可以使用 this.Session 来引用我需要的会话信息,该信息从 Global 继承的 HttpApplication 类访问 HttpApplication.Session 变量。但在我的应用程序中,我们自己在 Global 中重新定义了 Session 变量,如下所示:
public new static HttpSessionState Session
{
get
{
return HttpContext.Current.ApplicationInstance.Session;
}
}
因此,对我来说,引用 this.Session 将尝试从 HttpContext 中获取信息,该信息为 null。
所以我想我有两个问题。首先,是否有一个事件在 Session_End 之前但在会话数据被清除之前触发?其次,如果第一个问题的答案是否定的,是否还有办法从 Session_End 访问该数据?
谢谢。
I've already seen a similar question to this one posted here. I have essentially the same question, but with a small twist.
I'm trying to access Session information from within the Session_End method, but HttpContext is null by the time I get there, so I'm not finding any real way to get to it; it's almost like it's already been completely erased by the time Session_End fires.
A solution in the related question says I can use this.Session
to reference the session information I need, which accesses the HttpApplication.Session
variable from the HttpApplication class Global inherits from. But in my application, we've redefined the Session variable ourselves in Global to look like this:
public new static HttpSessionState Session
{
get
{
return HttpContext.Current.ApplicationInstance.Session;
}
}
So for me, referencing this.Session will try to get information out of HttpContext, which is null.
So I guess I have 2 questions. First, is there an event that fires before Session_End but before the Session data is cleared? And second, if the answer to the first question is no, is there still a way to access that data from Session_End?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,仅当您使用 in_proc 会话状态模式时,session_end 才会引发:如果您将 Web 应用程序部署在服务器场上,则不会引发该会话。
无论如何,即使我不知道你想要实现什么,最好在 HttpModule 中处理会话结束。看看下面的链接:
http://www.codeproject.com/KB/aspnet /SessionEndStatePersister.aspx
bear in mind the session_end will raised only if you are using in_proc session state mode: if you deploy your web app on a server farms it won't be raised.
anyway even if I don't know what you want achieve it would be better handle the session end within an HttpModule. have a look at the link below:
http://www.codeproject.com/KB/aspnet/SessionEndStatePersister.aspx