Global exception handling in static aspx [WebMethods] in ASP.NET

By default, the behavior of the [WebMethod] attribute associated with static on an aspx page is to return an error to the caller. We access these methods using json, and the only way to detect exceptions is to either try / catch in each web method on the site, or use the javascript callback with an error (which has an unacceptable flip side when exposing an error for the client).

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

+4
source share
1 answer

I do not know about health monitoring, but usually I have a common shell that executes endpoint code inside. This writes a real exception, but always generates a general exception across the border.

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
source

Source: https://habr.com/ru/post/1343963/


All Articles