カスタムエラーを使用してカスタムエラーページにリダイレクトを得ていない - ASP.Net

StackOverflow https://stackoverflow.com/questions/2509953

質問

ここに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の最も内側の例外を持っているならば、我々は403 AKAのステータスコードで新しいHTTPExcpetionを投げることがわかります。

ここでは、関連の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