gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:55:43 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com