未使用自定义错误重定向到自定义错误页面 - ASP.Net

发布于 2024-08-26 14:00:03 字数 1537 浏览 12 评论 0原文

这是我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫 2024-09-02 14:00:04

Application_Error 旨在捕获应用程序未处理的错误。当它触发时,错误已经发生,一切都与该错误有关。如果您从 Application_Error 中抛出错误,您实际上是在说“我的错误处理程序出现错误”。相反,只需 Server.Transfer 到适当的页面。如果您想将所有重定向逻辑保留在 web.config 中,您可以看到 这篇文章介绍了如何解析 customErrors 部分以找出重定向到的位置。

综上所述,我还没有尝试过,但是您可以尝试调用 Server.ClearError()

            Dim Ex = Server.GetLastError()
            Server.ClearError()
            Throw New HttpException(System.Net.HttpStatusCode.Forbidden, Nothing, Ex)

由于我上面所说的,我认为它不会起作用,但值得一试。

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 within Application_Error you are actually saying "there's an error with my error handler". Instead just Server.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():

            Dim Ex = Server.GetLastError()
            Server.ClearError()
            Throw New HttpException(System.Net.HttpStatusCode.Forbidden, Nothing, Ex)

I don't think it will work because of what I said above but its worth a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文