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 AKA 'forbidden'

这是相关的 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

不出所料,这也不起作用。

任何想法我们做错了什么?

4

1 回答 1

0

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)

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

于 2010-03-24T20:27:22.607 回答