未使用自定义错误重定向到自定义错误页面 - ASP.Net
这是我在 global.asax.vb 中的 Application_OnError 事件接收器:
Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs)
Dim innerMostException As Exception = getInnerMostException(Me.Context.Error)
If TypeOf innerMostException Is AccessDeniedException Then
Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException))
Dim fourOhThree As Integer = DirectCast(HttpStatusCode.Forbidden, Integer)
Throw New HttpException(fourOhThree, innerMostException.Message, innerMostException)
End If
End Sub
您会看到,如果我们有一个 AccessDeniedException 类型的最里面的异常,我们会抛出一个新的 HTTPExcpetion,其状态代码为 403,又称为“禁止”
这是相关的 web.config 条目:
<customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On">
<error statusCode="403" redirect="~/Secure/AccessDenied.aspx" />
</customErrors>
所以我们期望的是重定向到 AccessDenied.aspx 页面。我们得到的是到ServerError.aspx页面的重定向。
我们也尝试过这个:
Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs)
Dim innerMostException As Exception = getInnerMostException(Me.Context.Error)
If TypeOf innerMostException Is AccessDeniedException Then
Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException))
Context.Response.StatusCode = DirectCast(HttpStatusCode.Forbidden, Integer)
End If
End Sub
不出所料,这也不起作用。
有什么想法我们做错了吗?
Here's my Application_OnError event sink in global.asax.vb:
Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs)
Dim innerMostException As Exception = getInnerMostException(Me.Context.Error)
If TypeOf innerMostException Is AccessDeniedException Then
Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException))
Dim fourOhThree As Integer = DirectCast(HttpStatusCode.Forbidden, Integer)
Throw New HttpException(fourOhThree, innerMostException.Message, innerMostException)
End If
End Sub
You'll see that if we've got an innermost Exception of type AccessDeniedException we throw a new HTTPExcpetion with a status code of 403 AKA 'forbidden'
Here's the relevant web.config entry:
<customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On">
<error statusCode="403" redirect="~/Secure/AccessDenied.aspx" />
</customErrors>
So what we're expecting is a redirect to the AccessDenied.aspx page. What we get is a redirect to the ServerError.aspx page.
We've also tried this:
Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs)
Dim innerMostException As Exception = getInnerMostException(Me.Context.Error)
If TypeOf innerMostException Is AccessDeniedException Then
Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException))
Context.Response.StatusCode = DirectCast(HttpStatusCode.Forbidden, Integer)
End If
End Sub
Which unsuprisingly doesn't work either.
Any ideas what we're doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Application_Error
旨在捕获应用程序未处理的错误。当它触发时,错误已经发生,一切都与该错误有关。如果您从Application_Error
中抛出错误,您实际上是在说“我的错误处理程序出现错误”。相反,只需Server.Transfer
到适当的页面。如果您想将所有重定向逻辑保留在 web.config 中,您可以看到 这篇文章介绍了如何解析 customErrors 部分以找出重定向到的位置。综上所述,我还没有尝试过,但是您可以尝试调用
Server.ClearError()
:由于我上面所说的,我认为它不会起作用,但值得一试。
Application_Error
is intended to catch errors that aren't handled by your application. When it fires, an error has already happened and everything is about that error. If you throw an error from withinApplication_Error
you are actually saying "there's an error with my error handler". Instead justServer.Transfer
to the appropriate page. If you want to keep all of your redirection logic in web.config you can see this article about how to parse the customErrors section to figure out where to redirect to.All this said, and I haven't tried this, but can you try calling
Server.ClearError()
:I don't think it will work because of what I said above but its worth a try.