【发布时间】:2011-02-17 21:43:31
【问题描述】:
每当我们的网站发生未处理的异常时,我想:
- 发送通知电子邮件
- 清除用户的会话
- 将用户转到错误页面(“抱歉,出现问题...”)
第一个也是最后一个我已经工作了很长时间,但第二个给我带来了一些问题。我的 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 重定向似乎没有启动,我只剩下一个空白页?
有没有办法解决这个问题?
【问题讨论】:
-
作为一个子问题,有没有办法让 vb.net 代码在 Stack Overflow 上看起来不错,还是我不走运?
-
你试过用 Session.Abandon() 代替 Session.Clear() 吗?
标签: .net asp.net error-handling global-asax