javascript - 使用UpdatePanel时如何获取真正的异常消息?

标签 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: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500



我该怎么办才能使超时错误进入错误页面?

最佳答案

我终于在这里找到它: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

关于javascript - 使用UpdatePanel时如何获取真正的异常消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539940/

相关文章:

javascript - 具有数据库连接的 ASP.NET MVC 中的 HighChart

php - 使用多表从 PHP 填充 DataTables Ajax Json 对象

javascript - jQuery 选择器上的多个过滤器

c# - 在 ASP.NET Web Api C# 中通过流发送和接收大文件

html - IE窗口缩小时DIV溢出外层DIV(Restore-Down)

javascript - 我怎样才能让这个 javascript 调用我的 C# 方法并传递参数

php - jQuery 插件无法在加载了 ajax 的页面上运行

javascript - Google 在整个网络上的自定义搜索和限制 (gizoogle)

javascript - 多次点击 Protractor 不起作用

javascript - 修改范围变量后,范围绑定(bind)未更新