【问题标题】:Catch 404 Error in MVC4在 MVC4 中捕获 404 错误
【发布时间】:2013-02-18 16:19:50
【问题描述】:

我在处理 HTTP 错误 404.0 - 未找到时遇到问题。我已经打开了

<customErrors mode="On" defaultRedirect="~/Error/General">
      <error statusCode="404" redirect="~/Error/HttpError404" />
      <error statusCode="500" redirect="~/Error/HttpError500" />
    </customErrors>

在 Web.Config 中。但问题依然存在。我也试过这个解决方案(但它从来没有达到方法):

protected void Application_Error()
    {
      var exception = Server.GetLastError();
      var httpException = exception as HttpException;
      Response.Clear();
      Server.ClearError();
      var routeData = new RouteData();
      routeData.Values["controller"] = "Errors";
      routeData.Values["action"] = "General";
      routeData.Values["exception"] = exception;
      Response.StatusCode = 500;
      if (httpException != null)
      {
        Response.StatusCode = httpException.GetHttpCode();
        switch (Response.StatusCode)
        {
          case 403:
            routeData.Values["action"] = "HttpError404";
            break;
          case 404:
            routeData.Values["action"] = "HttpError404";
            break;
        }
      }

      IController errorsController = new ErrorController();
      var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
      errorsController.Execute(rc);
    }

@Darin Dimitrov提供

这是控制器:

public class ErrorController : Controller
  {

    public ActionResult HttpError404(string error)
    {
      ViewData["Title"] = "Sorry, an error occurred while processing your request. (404)";
      ViewData["Description"] = error;
      return View("Index");
    }

    public ActionResult HttpError500(string error)
    {
      ViewData["Title"] = "Sorry, an error occurred while processing your request. (500)";
      ViewData["Description"] = error;
      return View("Index");
    }


    public ActionResult General(string error)
    {
      ViewData["Title"] = "Sorry, an error occurred while processing your request.";
      ViewData["Description"] = error;
      return this.View();

    }

【问题讨论】:

    标签: asp.net-mvc-4 iis-8


    【解决方案1】:

    好的,感谢@alistair-findlay 和this website,我找到了解决方案。 这就是 web.config 现在的样子:

    <system.web>
        <customErrors mode="On" defaultRedirect="~/Error/General" redirectMode="ResponseRewrite"> 
            </customErrors>
     </system.web>
      <system.webServer>
    <httpErrors errorMode="Detailed" defaultResponseMode="Redirect">
          <clear/>
          <error statusCode="404" path="/Error/HttpError404"/>
        </httpErrors>
    
      </system.webServer
    

    这是 Global.asax.cs:

      protected void Application_Error()
        {
    
          if (Context.IsCustomErrorEnabled)
            ShowCustomErrorPage(Server.GetLastError());
    
        }
        private void ShowCustomErrorPage(Exception exception)
        {
          var httpException = exception as HttpException ?? new HttpException(500, "Internal Server Error", exception);
    
          Response.Clear();
          var routeData = new RouteData();
          routeData.Values.Add("controller", "Error");
          routeData.Values.Add("fromAppErrorEvent", true);
    
          switch (httpException.GetHttpCode())
          {
            case 403:
              routeData.Values.Add("action", "HttpError403");
              break;
    
            case 404:
              routeData.Values.Add("action", "HttpError404");
              break;
    
            case 500:
              routeData.Values.Add("action", "HttpError500");
              break;
    
            default:
              routeData.Values.Add("action", "GeneralError");
              routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
              break;
          }
    
          Server.ClearError();
    
          IController controller = new ErrorController();
          controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }
    

    最后:

     public class ErrorController : Controller
      {
    
        public ActionResult HttpError403(string error)
        {
          ViewBag.Description = error;
          return this.View();
        }
    

    【讨论】:

    【解决方案2】:

    您是否实现了 ErrorController 来处理 customErrors 属性中的重定向?在您的情况下,它看起来像这样:

    public class ErrorController : Controller
    {
        public ActionResult General()
        {
            return View();
        }
    
        public ActionResult HttpError404()
        {
            return View();
        }
    
        public ActionResult HttpError500()
        {
            return View();
        }
    }
    

    您还需要为 Views 文件夹中的每个 ActionResult 实现视图。

    在记录错误方面,您需要在Application_Error 事件处理程序中实现这一点,如下所示:

    protected void Application_Error()
    {
        var exception = Server.GetLastError();
        var httpException = exception as HttpException;
        // Do logging here
    }
    

    这是为了在 302 重定向到上面ErrorController 中定义的错误页面之前“捕获”错误。

    【讨论】:

    • 是的,我已经实现了 ErrorController()(我已将它添加到我的问题中)。
    • 当您导航到一个不存在的页面时,您会看到什么? “黄屏死机”还是 HttpError404 页面?
    • 我添加了我正在获取的页面的场景镜头。 :D
    • 不知道这篇文章中的一些建议是否有帮助?... stackoverflow.com/questions/6308186/…>
    猜你喜欢
    • 2013-12-04
    • 2010-11-21
    • 2018-01-16
    • 2021-11-09
    • 2012-05-10
    • 1970-01-01
    • 2017-04-17
    • 2011-02-10
    相关资源
    最近更新 更多