2017-07-13 49 views
0

我已经在我的应用程序中使用此后Link中的解决方案将错误处理拼接在一起。我得到的问题是,该应用程序仍然路由到默认的错误页面。代码一直到Global.asax,当我断点时,但是当视图自定义错误页面应该加载时,它会从解决方案加载默认的错误页面。我试图删除默认的错误页面,然后我从IIS获取了黄色的错误页面。不知疲倦地搜索了网页,但没有结果。感谢所有的帮助。如果您认为我可以更好地调整问题或标题,我乐于接受建议。自定义错误处理程序仍然加载默认错误页面

误差控制器:

public class ErrorController : Controller 
{ 
    public ActionResult PageNotFound(Exception ex) 
    { 
     Response.StatusCode = 404; 
     return View("Error", ex); 
    } 

    public ActionResult ServerError(Exception ex) 
    { 
     Response.StatusCode = 500; 
     return View("Error", ex); 
    } 

    public ActionResult UnauthorisedRequest(Exception ex) 
    { 
     Response.StatusCode = 403; 
     return View("Error", ex); 
    } 

    //Any other errors you want to specifically handle here. 

    public ActionResult CatchAllUrls() 
    { 
     //throwing an exception here pushes the error through the Application_Error method for centralised handling/logging 
     throw new HttpException(404, "The requested url " + Request.Url.ToString() + " was not found"); 
    } 
} 

代码在Global.asax.cs中:

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception exception = Server.GetLastError(); 

     //Error logging omitted 

     HttpException httpException = exception as HttpException; 
     RouteData routeData = new RouteData(); 
     IController errorController = new Controllers.ErrorController(); 
     routeData.Values.Add("controller", "Error"); 
     routeData.Values.Add("area", ""); 
     routeData.Values.Add("ex", exception); 

     if (httpException != null) 
     { 
      //this is a basic example of how you can choose to handle your errors based on http status codes. 
      switch (httpException.GetHttpCode()) 
      { 
       case 404: 
        Response.Clear(); 

        // page not found 
        routeData.Values.Add("action", "PageNotFound"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 

        break; 
       case 500: 
        // server error 
        routeData.Values.Add("action", "ServerError"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
        break; 
       case 403: 
        // server error 
        routeData.Values.Add("action", "UnauthorisedRequest"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
        break; 
       //add cases for other http errors you want to handle, otherwise HTTP500 will be returned as the default. 
       default: 
        // server error 
        routeData.Values.Add("action", "ServerError"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
        break; 
      } 
     } 
     //All other exceptions should result in a 500 error as they are issues with unhandled exceptions in the code 
     else 
     { 
      routeData.Values.Add("action", "ServerError"); 
      Server.ClearError(); 
      // Call the controller with the route 
      errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
     } 
    } 

RouteConfig.cs

routes.MapRoute("CatchAllUrls", "{*url}", new {controller = "Error", action = "CatchAllUrls"}); 

catch块:

throw new HttpException(404, "Unable to write to list" + ", " + e.InnerException); 
+0

你在web.config的''部分有''? –

+0

这是您的ErrorController get执行?我认为你应该重定向。见[本文](https://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine)。 – miszczak

+0

你认为六个例子中的哪一个适合我? – AllramEst

回答

0

在@GeorgPatscheider和@AliAshraf的帮助下,我终于解决了我的问题。我通过改变这个返回视图(“Error”,ex.Message)来实现它的工作。到这个返回视图(“Error”,ex);并添加了<customErrors mode="On"/>。现在我还可以添加更多的HTTP错误代码。我在最初的帖子后添加了Messageex谢谢所有帮助!

相关问题