【问题标题】:Custom Error Page Being Sent With Ajax Response in ASP.NET MVC 3在 ASP.NET MVC 3 中使用 Ajax 响应发送自定义错误页面
【发布时间】:2023-03-12 20:28:01
【问题描述】:

为什么发生错误时,自定义错误页面会与下面的 ajax 响应一起发送?

回应

{"Errors":["An error has occurred and we have been notified.  We are sorry for the inconvenience."]}<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Error</title>

Web.Config

 <customErrors defaultRedirect="Error" mode="On"></customErrors>

BaseController.cs

public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                var response = filterContext.HttpContext.Response;

                var validatorModel = new ValidatorModel();

                if (filterContext.Exception is AriesException && !((AriesException)filterContext.Exception).Visible && filterContext.HttpContext.IsCustomErrorEnabled)
                {
                    validatorModel.Errors.Add(this.Resource("UnknownError"));
                }
                else
                {
                    validatorModel.Errors.Add(filterContext.Exception.Message);
                }

                response.Clear();
                response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                response.Write(validatorModel.ToJson());
                response.ContentType = "application/json";
                response.TrySkipIisCustomErrors = true;
                filterContext.ExceptionHandled = true;
                System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            else if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                filterContext.ExceptionHandled = true;
            }

            if(filterContext.ExceptionHandled)
            {
                SiteLogger.Write(filterContext.Exception);
            }
        }


   }

【问题讨论】:

    标签: asp.net-mvc error-handling


    【解决方案1】:

    如果有人仍然有这个问题,我找到了一个稍微干净的解决方案:

    if (!filterContext.HttpContext.Request.IsAjaxRequest())
    {
            //non-ajax exception handling code here
    }
    else
    {
            filterContext.Result = new HttpStatusCodeResult(500);
            filterContext.ExceptionHandled = true;
    }
    

    【讨论】:

      【解决方案2】:

      dsomuah 的解决方案很好,但必须添加到每个为 Ajax 请求提供服务的控制器中。我们通过全局注册以下操作过滤器更进一步:

      public class HandleAjaxErrorAttribute : HandleErrorAttribute
      {
          public override void OnException(ExceptionContext filterContext)
          {
              if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
              {
                  filterContext.ExceptionHandled = true;
                  filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                  filterContext.HttpContext.Response.StatusDescription = filterContext.Exception.Message;
              }
          }
      }
      

      【讨论】:

      • 虽然 dsomuah 的代码可以很容易地进入你的控制器基类,因为许多项目已经实现了各种原因,很高兴看到这里列出了这个方法。 +1
      【解决方案3】:

      我添加了 response.End();它奏效了。有没有更好的办法?

      【讨论】:

        猜你喜欢
        • 2011-08-13
        • 2013-06-08
        • 2011-10-07
        • 2016-06-22
        • 2018-12-03
        • 2014-06-27
        • 1970-01-01
        • 2011-09-24
        • 2015-11-14
        相关资源
        最近更新 更多