使用UpdatePanel时如何获得真正的异常消息?

时间:2011-06-30 19:18:05

标签: javascript asp.net ajax webforms error-handling

使用UpdatePanel时,我没有在自定义错误页面中看到真正的错误。在我的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.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:   发生未知错误   在服务器上处理请求。   从中返回的状态代码   服务器是:500

如何将超时错误发送到错误页面?

2 个答案:

答案 0 :(得分:1)

我终于在这里找到了它:http://msdn.microsoft.com/en-us/library/bb398934.aspx

虽然他们的例子不会编译,也没有完全按照我的要求给我,但它让我朝着正确的方向前进。我能够使用以下代码控制放入Async异常中的消息:

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

我还必须删除web.config中的defaultRedirect,并且如果它不是异步调用则仅有条件地重定向。我通过将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

答案 1 :(得分:0)

我碰到了类似的东西。您大多数情况下都是正确的,但您仍然可以在web.config中使用自定义错误属性,只需在脚本管理器上指示使用此属性忽略它:AllowCustomErrorsRedirect =“false”