无法使用自定义错误重定向到自定义错误页-ASP.Net

无法使用自定义错误重定向到自定义错误页-ASP.Net,asp.net,vb.net,web-config,httpexception,Asp.net,Vb.net,Web Config,Httpexception,下面是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 T

下面是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
您将看到,如果我们有AccessDeniedException类型的最内部异常,我们将抛出一个状态代码为403又称“禁止”的新HttpException

以下是相关的web.config条目:

    <customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On">
      <error statusCode="403" redirect="~/Secure/AccessDenied.aspx" />
    </customErrors>    
这也不起作用


知道我们做错了什么吗?

应用程序错误旨在捕获应用程序无法处理的错误。当它触发时,一个错误已经发生,一切都与该错误有关。如果您从
应用程序\u error
中抛出错误,实际上是在说“我的错误处理程序有错误”。只需
服务器。将
转移到相应页面。如果要将所有重定向逻辑保留在web.config中,可以查看如何解析customErrors部分,以确定重定向到何处

所有这些都说了,我还没有试过,但是你能试着调用
Server.ClearError()


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

是的,我想我对通过web.config中的“自定义错误”部分使用重定向有一点幻想,并放弃了仅仅执行server.transfer。谢谢你的帮助。
    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
            Dim Ex = Server.GetLastError()
            Server.ClearError()
            Throw New HttpException(System.Net.HttpStatusCode.Forbidden, Nothing, Ex)