【问题标题】:Globally handling exceptions in static aspx [WebMethods] in ASP.NET在 ASP.NET 中全局处理静态 aspx [WebMethods] 中的异常
【发布时间】:2011-03-16 17:07:06
【问题描述】:

aspx 页面上的 [WebMethod] 属性静态方法的默认行为是将错误返回给调用者。我们正在使用 json 访问这些方法,我们发现捕获异常的唯一方法是在站点上的每个 web 方法中使用 try/catch 或使用带有错误的 javascript 回调(这具有将错误暴露给客户)。

有没有办法使用 ASP.NET 中的 HealthMonitoring 设置来全局处理这些异常?

【问题讨论】:

    标签: asp.net exception-handling webmethod pagemethods health-monitoring


    【解决方案1】:

    我不了解健康监控,但我通常有一个通用包装器来执行内部端点代码。这会记录真正的异常,但总是会跨边界抛出通用异常。

    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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 2018-10-09
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2019-07-18
      • 2017-02-24
      相关资源
      最近更新 更多