【问题标题】:How to handle 404 error in config and code in MVC5?如何处理 MVC5 中配置和代码中的 404 错误?
【发布时间】:2016-09-02 06:11:44
【问题描述】:

我已经实现了下面链接中提到的异常处理

How to pass error message to error view in MVC 5?

它工作正常。但我需要处理404 Error

我该怎么做?

如果我使用下面的代码,

<customErrors mode="On">
  <error statusCode="404" redirect="/Home/Error"></error>
</customErrors>

当出现任何404 错误时,它运行良好。但是如果发生任何其他异常,那么我的error.cshtml 调用两次并显示相同的异常two times

【问题讨论】:

    标签: asp.net-mvc exception-handling


    【解决方案1】:

    web.config

    关闭 system.web 中的自定义错误

    <system.web>
        <customErrors mode="Off" />
    </system.web>
    

    在 system.webServer 中配置 http 错误

    <system.webServer>
        <httpErrors errorMode="Custom" existingResponse="Auto">
          <clear />
          <error statusCode="404" responseMode="ExecuteURL" path="/NotFound" />
          <error statusCode="500" responseMode="ExecuteURL" path="/Error" />
        </httpErrors>
    </system.webServer>
    

    创建简单的错误控制器来处理这些请求ErrorContoller.cs

    [AllowAnonymous]
    public class ErrorController : Controller {
        // GET: Error
        public ActionResult NotFound() {
            var statusCode = (int)System.Net.HttpStatusCode.NotFound;
            Response.StatusCode = statusCode;
            Response.TrySkipIisCustomErrors = true;
            HttpContext.Response.StatusCode = statusCode;
            HttpContext.Response.TrySkipIisCustomErrors = true;
            return View();
        }
    
        public ActionResult Error() {
            Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            Response.TrySkipIisCustomErrors = true;
            return View();
        }
    }
    

    配置路由RouteConfig.cs

    public static void RegisterRoutes(RouteCollection routes) {
    
        //...other routes 
    
        routes.MapRoute(
            name: "404-NotFound",
            url: "NotFound",
            defaults: new { controller = "Error", action = "NotFound" }
        );
    
        routes.MapRoute(
            name: "500-Error",
            url: "Error",
            defaults: new { controller = "Error", action = "Error" }
        );
    
        //..other routes
    
        //I also put a catch all mapping as last route
    
        //Catch All InValid (NotFound) Routes
        routes.MapRoute(
            name: "NotFound",
            url: "{*url}",
            defaults: new { controller = "Error", action = "NotFound" }
        );
    }
    

    最后确保您有控制器操作的视图

    Views/Shared/NotFound.cshtml
    Views/Shared/Error.cshtml
    

    如果您想要处理任何其他错误,您可以按照该模式并根据需要添加。这将避免重定向并保持引发的原始 http 错误状态。

    【讨论】:

    • 除了stackoverflow.com/questions/39246370/…,我也跟着你对这个问题的回答...当404出现时,效果很好
    • 但是,如果出现其他异常,错误页面中的模型将显示为 null,因为 Error() 操作触发了我认为不会发送任何信息来查看
    • 检查服务器上一个错误,看看你是否可以在那里建立模型
    • 嗨... GetLastError() 不起作用。所以我使用了会话,它对我来说效果很好......如果有任何最好的方法可以做到这一点。在 Error.cshtml 中,如果 (Session["Error"] != null) { Session.Remove("Error"); } else { 会话[“错误”] = 模型; }
    • 在errorcontroller中,if (Session["Error"]!=null) { HandleErrorInfo obj =(HandleErrorInfo) Session["Error"];返回视图(obj); }
    【解决方案2】:

    如果您要为 customErrors 定义 defaultRedirect 属性,那么在您的情况下,error.cshtml 将只呈现一次:

     <customErrors mode="On" defaultRedirect="/Home/Error">
              <error statusCode="404" redirect="/Home/Error"/>
     </customErrors>
    

    【讨论】:

      猜你喜欢
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 2022-07-14
      • 2011-11-20
      相关资源
      最近更新 更多