我有一种情况,我使用 jQuery 在 .aspx 代码隐藏(不是 asmx webservice 或类似的东西)中调用 Webmethod。我在 Global.asax ApplicationError 中定义了一个简单的错误记录器,因此(几乎)所有异常都被捕获并记录在一个地方。这里的问题是,如果 web 方法内部发生异常,则不会到达 ApplicationError 方法。
这是一个简单的例子:
网络方法调用:
$.ajax({
type: "POST",
url: "/MyMethods.aspx/MyMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert('ok');
}
});
网络方法实现:
Public Class MyMethods
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()> _
Public Shared Function MyMethod() As String
Throw New Exception("Forced.")
Return sb.ToString
End Function
End Class
和 global.asax
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim filename As String = "d:\test.txt"
Using objWriter As New System.IO.StreamWriter(filename, True)
objWriter.WriteLine(Server.GetLastError().GetBaseException().Message)
End Using
End Sub
当 webmethod 抛出异常时,有什么方法可以“说服”到达 ApplicationError 方法?我已经尝试过(字面上)数百种场景:
- 埃尔玛
- http://geekswithblogs.net/pavelka/archive/2005/09/05/HowToCreateAGlobalExceptionHandlerForAWebService.aspx
http://blog.theobjectguy.com/2009/12/exceptional-gotchas.html
这里有很多问题...
...但到目前为止,他们都没有工作。因此,我们将不胜感激任何帮助!