开发者

Globally handling exceptions in static aspx [WebMethods] in ASP.NET

The default behavior of a [WebMethod] attributed static method on an aspx page is to return the error to the caller. We are accessing these methods using json, and the only way we have found of capturing exceptions is either a try/catch in every webmethod on the site or using a javascript callback with th开发者_如何学Ce error (which has the unacceptable downside of exposing the error to the client).

Is there any way to globally handle these exceptions using the HealthMonitoring setup in ASP.NET?


I don't know about health monitoring, but I normally have a generic wrapper that executes the endpoint code inside. This records the real exception but always throws a generic exception across the boundry.

public static T wrapAjaxRequestsToCatchException<T>(Func<T> wrappedDelegate) where T : JsonBase, new()
    {
        try
        {
            return wrappedDelegate();
        }
        catch (Exception ex)
        {
            var errResponse = new T()
            {
                Success = false,
                Message = getErrorMessage(ex)
            };
            // Log the exception
            ErrorLog.LogAjaxEvent(string.Format("AJAX EXCEPTION : {0}", ex.ToString()), System.Diagnostics.EventLogEntryType.Error);
            return errResponse;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜