2011-06-30 27 views
1

使用UpdatePanel時,我在自定義錯誤頁面中看不到真正的錯誤。在我的Global.asax我有以下代碼:如何在使用UpdatePanel時獲得真正的異常消息?

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) 

    'Get the exception 
    Dim lastError = Server.GetLastError() 
    If lastError IsNot Nothing Then 
     Dim ex As Exception = lastError.GetBaseException() 
     If ex IsNot Nothing Then 
      'Log the error 
      If Log.IsErrorEnabled Then 
       log4net.ThreadContext.Properties("method") = ex.TargetSite.Name 
       log4net.ThreadContext.Properties("userId") = User.Current.UserName 
       Log.Error(ex.Message, ex) 
      End If 
     End If 
    End If 

End Sub 

如果我查看日誌或設置斷點,我可以看到,我得到一個超時問題。然後,我有以下的代碼到用戶發送到錯誤頁面,並顯示錯誤:

<script language="javascript" type="text/javascript"> 
    function EndRequestHandler(sender, args) { 
     if (args.get_error() != undefined) { 
      // Let the framework know that the error is handled, 
      // so it doesn't throw the JavaScript alert. 
      args.set_errorHandled(true); 

      var errorMessage = args.get_error().message.replace(/</gi, "&lt;").replace(/>/gi, "&gt;"); 

      // If there is, show the custom error. 
      window.location = _root + 'ShowError.aspx?error=' + encodeURI(errorMessage) 
     } 
    } 
</script> 

但我從args.get_error(得到的錯誤)是這個:

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

我需要做什麼才能使錯誤頁面發生超時錯誤?

+0

另外,我有customErrors mode = 「開」,如果我添加defaultRedirect =「ShowError.aspx」狀態代碼從500更改爲0,但我仍然收到錯誤。 – adam0101

回答

1

我終於找到了這裏:http://msdn.microsoft.com/en-us/library/bb398934.aspx

雖然他們的將不會是編譯並沒有給我我需要的正是它讓我在正確的方向。我能夠控制工具,使用此代碼放在異步異常的消息:

Protected Sub ScriptManager1_AsyncPostBackError(ByVal sender As Object, ByVal e As System.Web.UI.AsyncPostBackErrorEventArgs) 
    If e.Exception.Data("ExtraInfo") IsNot Nothing Then 
     ScriptManager1.AsyncPostBackErrorMessage = _ 
      e.Exception.Message & _ 
      e.Exception.Data("ExtraInfo").ToString() 
    Else 
     ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message 
    End If 
End Sub 

我也不得不刪除的defaultRedirect在web.config中,只有有條件地重定向,如果它不是一個異步調用。我通過將Global.asax更改爲:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) 

    'Get the exception 
    Dim lastError = Server.GetLastError() 
    If lastError IsNot Nothing Then 
     Dim ex As Exception = lastError.GetBaseException() 
     If ex IsNot Nothing Then 
      'Log the error 
      If Log.IsErrorEnabled Then 
       log4net.ThreadContext.Properties("method") = ex.TargetSite.Name 
       log4net.ThreadContext.Properties("userId") = User.Current.DomainName 
       Log.Error(ex.Message, ex) 
      End If 
      If Not IsAsyncPostBackRequest(Request) Then 
       Server.Transfer("~/ShowError.aspx") 
      End If 
     End If 
    End If 

End Sub 

Public Function IsAsyncPostBackRequest(request As HttpRequest) As Boolean 
    Dim values = request.Headers.GetValues("X-MicrosoftAjax") 
    If values IsNot Nothing Then 
     For Each value In values 
      Dim parts = value.Split(","c) 
      For Each part In parts 
       If part.Trim() = "Delta=true" Then 
        Return True 
       End If 
      Next 
     Next 
    End If 
    Return False 
End Function 
0

我遇到了一些類似的東西。你大部分都是正確的,但是你仍然可以在web.config中使用自定義錯誤屬性,只需在你的腳本管理器上指示忽略它就可以使用這個屬性:AllowCustomErrorsRedirect =「false」