2017-07-13 4 views
0

Ich habe eine Fehlerbehandlung für meine Anwendung unter Verwendung einer Lösung in diesem Beitrag Link genäht. Das Problem, das ich bekomme, ist, dass die App immer noch auf die Standardfehlerseite routet. Der Code geht bis zum Global.asax, wenn ich es aufhalte, aber wenn die Ansicht benutzerdefinierte Fehlerseite geladen werden soll, wird die Standardfehlerseite aus der Lösung geladen. Ich habe versucht, die Standardfehlerseite zu entfernen, dann bekam ich die gelbe Fehlerseite von IIS. Unermüdlich im Internet gesucht, aber ohne Ergebnis. Dankbar für all die Hilfe. Wenn Sie denken, dass ich die Frage oder den Titel verbessern kann, bin ich offen für Vorschläge.Angepasster Error-Handler lädt immer noch die Standardfehlerseite

Fehler Controller:

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"); 
    } 
} 

-Code in 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"}); 

Der catch-Block:

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

Haben Sie '' im Abschnitt '' von web.config? –

+0

Wird Ihr ErrorController get ausgeführt? Ich denke, du solltest stattdessen umleiten. Siehe [diesen Artikel] (https://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine). – miszczak

+0

Welches der sechs Beispiele passt meiner Meinung nach am besten zu mir? – AllramEst

Antwort

0

Mit Hilfe von @GeorgPatscheider und @AliAshraf habe ich mein Problem endlich gelöst.Ich habe es geschafft, indem ich diese Rückkehransicht ändere ("Error", ex.Message); zu dieser Rückkehr Ansicht ("Error", ex); und hinzugefügt . Jetzt kann ich auch zusätzliche HTTP-Fehlercodes hinzufügen. Ich hatte Messageex nach dem ersten Beitrag hinzugefügt Vielen Dank für die Hilfe !!!

Verwandte Themen