下面是在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“禁止”

的状态码

下面是有关的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

哪些unsuprisingly也不起作用。

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

有帮助吗?

解决方案

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)

我不认为它会因为我上面所说的,但它值得一试说的工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top