2

jQuery を使用して、.aspx コードビハインド (asmx Web サービスなどではない) で Webmethod を呼び出す状況があります。Global.asax ApplicationError で単純なエラー ロガーを定義しているため、(ほぼ) すべての例外がキャッチされ、1 か所に記録されます。ここで問題なのは、webmethod 内で例外が発生した場合、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 メソッドに到達するように「説得」する方法はありますか? 私は(文字通り)何百ものシナリオを試しました:

...しかし、今のところどれも機能していません。ですから、どんな助けでも大歓迎です!

4

0 に答える 0