【问题标题】:How to get real exception message when using UpdatePanel?使用 UpdatePanel 时如何获取真正的异常消息?
【发布时间】:2011-09-26 06:21:42
【问题描述】:

使用 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

我要怎么做才能让超时错误显示到错误页面?

【问题讨论】:

  • 另外,我有 customErrors mode="On" 如果我​​添加 defaultRedirect="ShowError.aspx" 状态码从 500 变为 0,但我仍然收到错误。

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


【解决方案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

我还必须删除 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

【讨论】:

    【解决方案2】:

    我遇到了一些类似的东西。您基本上是正确的,但您仍然可以在 web.config 中使用您的自定义错误属性,只需在脚本管理器上使用此属性指示忽略它:AllowCustomErrorsRedirect="false"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-30
      • 2014-06-14
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多