2010-03-24 67 views
0

這裏是我的Application_OnError事件接收器在global.asax.vb:使用自定義的錯誤沒有得到重定向到自定義錯誤頁 - ASP.Net

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與AKA「禁止」

下面是相關的web.config條目403狀態碼:

<customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On"> 
     <error statusCode="403" redirect="~/Secure/AccessDenied.aspx" /> 
    </customErrors>  

所以我們正在期待是重定向到t他訪問了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也不起作用。

任何想法我們做錯了什麼?

回答

0

Application_Error旨在趕上不是由您的應用程序處理的錯誤。當它發生火災時,一個錯誤已經發生,一切都與這個錯誤有關。如果你從Application_Error中發現錯誤,你實際上是在說「我的錯誤處理程序有錯誤」。取而代之的只是Server.Transfer到適當的頁面。如果你想保留所有重定向邏輯在web.config中你可以看到this article如何解析的customErrors部分揣摩出重定向到。

這一切說,我沒有試過,但你可以嘗試調用Server.ClearError()

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

我不認爲它會因爲我上面所說的,但它值得一試說的工作。

+0

是啊,我想我有點隧道visioned有關通過web.config中的自定義錯誤部分使用重定向,並駁回只是在做一個Server.Transfer的。謝謝您的幫助。 – user129345 2010-03-25 10:20:42