【问题标题】:ASP.NET MVC5 always returns HTTP500 regardless of exception when <customErrors> is turned onASP.NET MVC5 在打开 <customErrors> 时始终返回 HTTP500,无论是否出现异常
【发布时间】:2014-10-05 01:37:59
【问题描述】:

设置:

  • HandleErrorAttribute 已被移除并清除所有过滤器。
  • Global.asax 中未捕获或处理异常

IIS 错误的 web.config 设置如下:

<httpErrors errorMode="Custom" existingResponse="Replace" defaultPath="/StaticErrors/Default.html" defaultResponseMode="ExecuteURL">
         <clear />
         <error statusCode="400" path="/myCustomErrors/Http400" responseMode="ExecuteURL" />
         <error statusCode="401" path="/myCustomErrors/Http401" responseMode="ExecuteURL" />
         <error statusCode="403" path="/myCustomErrors/Http403" responseMode="ExecuteURL" />
         <error statusCode="404" path="/myCustomErrors/Http404" responseMode="ExecuteURL" />
         <error statusCode="500" path="/myCustomErrors/Http500" responseMode="ExecuteURL" />
      </httpErrors>

这正常工作,但是,如果我添加以下 customErrors 代码 - 那么任何错误,例如错误请求或未找到将总是导致错误 500 页面显示。由于某种原因,HttpException 被视为未处理的异常。

<customErrors defaultRedirect="/StaticErrors/Default.html" mode="On" redirectMode="ResponseRewrite">
         <error redirect="~/myCustomErrors/Http400" statusCode="400" />
         <error redirect="~/myCustomErrors/Http401" statusCode="401" />
         <error redirect="~/myCustomErrors/Http403" statusCode="403" />
         <error redirect="~/myCustomErrors/Http404" statusCode="404" />
         <error redirect="~/myCustomErrors/Http500" statusCode="500" />
   </customErrors>

在仅 MVC 的应用程序中以这种方式禁用 ASP.NET 错误处理会影响哪些问题。

【问题讨论】:

  • 尝试删除~
  • 有或没有 ~ 没有任何区别。我认为加载基于 MVC 的异常页面可能存在进一步的错误。但是失败的请求跟踪什么也没显示。直接导航到 MVC 错误页面也可以正常工作。包含 customErrors 时,始终执行“/myCustomErrors/Http500”。
  • 在您的自定义错误中尝试500.100 状态代码而不是500

标签: asp.net asp.net-mvc exception-handling


【解决方案1】:

最后我只能通过使用 Global.asax 文件和 Application_Error 方法有效地重写默认错误处理能力来解决这个问题。

protected void Application_Error()
      {
         var errorCodeToUse = 500;

         var currentException = Server.GetLastError();
         if (currentException != null)
         {
            var httpException = currentException as System.Web.HttpException;

            if (httpException != null)
            {
               errorCodeToUse = httpException.GetHttpCode();
            }
         }

         var currentContext = HttpContext.Current;
         RequestContext requestContext = null;

         if (currentContext.CurrentHandler != null)
         {
            requestContext = ((MvcHandler)currentContext.CurrentHandler).RequestContext;
         }

         if (requestContext == null)
         {
            requestContext = new RequestContext(new HttpContextWrapper(Context), new RouteData());
         }

         requestContext.RouteData.Values["controller"] = "Error";

         switch (errorCodeToUse)
         {
            case 400:
               requestContext.RouteData.Values["action"] = "Http400";
               break;
            case 401:
               requestContext.RouteData.Values["action"] = "Http401";
               break;
            case 403:
               requestContext.RouteData.Values["action"] = "Http403";
               break;
            case 404:
               requestContext.RouteData.Values["action"] = "Http404";
               break;
            case 405:
               requestContext.RouteData.Values["action"] = "Http405";
               break;
            case 429:
               requestContext.RouteData.Values["action"] = "Http429";
               break;
            default:
               requestContext.RouteData.Values["action"] = "Http500";
               break;
         }

         var factory = ControllerBuilder.Current.GetControllerFactory();
         var controller = factory.CreateController(requestContext, "Error");
         controller.Execute(requestContext);
         currentContext.Server.ClearError();

      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 2018-10-10
    • 2014-05-16
    • 2010-09-08
    • 2017-05-13
    相关资源
    最近更新 更多