清除全局Application_Error中的会话
,我想要:
- 发送通知电子邮件
- 清除用户的会话
- 将用户发送到错误页面(“抱歉,发生了问题...”)
每当我们的网站上发生未处理的异常时 很长一段时间,但第二个给我带来了一些问题。我的 Global.asax.vb 包括:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Send exception report
Dim ex As System.Exception = Nothing
If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Server IsNot Nothing Then
ex = HttpContext.Current.Server.GetLastError
End If
Dim eh As New ErrorHandling(ex)
eh.SendError()
' Clear session
If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Session IsNot Nothing Then
HttpContext.Current.Session.Clear()
End If
' User will now be sent to the 500 error page (by the CustomError setting in web.config)
End Sub
当我运行调试时,我可以看到会话被清除,但在下一页上会话又回来了!
我最终找到了一个参考文献,表明除非调用 Server.ClearError ,否则不会保存对会话的更改。不幸的是,如果我添加这个(就在设置“ex”的行下方),那么 CustomErrors 重定向似乎不会启动,我会留下一个空白页面?
有办法解决这个问题吗?
Whenever an unhandled exception occurs on our site, I want to:
- Send a notification email
- Clear the user's session
- Send the user to a error page ("Sorry, a problem occurred...")
The first and last I've had working for a long time but the second is causing me some issues. My Global.asax.vb includes:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Send exception report
Dim ex As System.Exception = Nothing
If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Server IsNot Nothing Then
ex = HttpContext.Current.Server.GetLastError
End If
Dim eh As New ErrorHandling(ex)
eh.SendError()
' Clear session
If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Session IsNot Nothing Then
HttpContext.Current.Session.Clear()
End If
' User will now be sent to the 500 error page (by the CustomError setting in web.config)
End Sub
When I run a debug, I can see the session being cleared, but then on the next page the session is back again!
I eventually found a reference that suggests that changes to session will not be saved unless Server.ClearError is called. Unfortunately, if I add this (just below the line that sets "ex") then the CustomErrors redirect doesn't seem to kick in and I'm left with a blank page?
Is there a way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Brunis - 在回答您的问题时(我遇到了同样的问题),您的应用程序可能在您有权访问会话信息(例如 Begin_Request 阶段)之前的某个时间点崩溃到 application_error。
Brunis - In answer to your question (I had the same problem) your application may have crashed out to application_error at a point before you have access to the session information, such as Begin_Request stage.
您可以清除自定义错误页面上的会话吗?
另外,您可能想考虑使用 ELMAH。它使您想做的所有事情变得简单而精彩。
Can you clear the session on your custom error page?
Also, you might want to look into using ELMAH. It makes all the stuff you are trying to do easy and awesome.