2013-02-27 3 views
1

MVC (4)에서 오류 처리를 설정하고 잘 작동합니다. global.asax에 HandleErrorAttribute를 등록하고 web.config에 apropriate 구성을 설정했습니다. 그러나 오류보기로 리디렉션하고 오류보기 자체에서 오류가 발생하면 오류 페이지로 다시 리디렉션됩니다. 레이아웃에서 오류가 발생하고 응용 프로그램 외부에서 레이아웃이 관리됩니다. 레이아웃에 오류가있을 경우 나는 묶었습니다. 이 문제를 어떻게 방지 할 수 있습니까? 어떤 종류의 오류 처리 오류를 사용해야합니까? 다른 레이아웃을 사용하는 것은 옵션이 아닙니다.Asp.Net MVC : 오류보기에서 처리 오류가 발생했습니다.

+0

오류가 발생하면 디버깅하여 global.asax에서 'Application_Error'가 발생했는지 확인하십시오. – mattytommo

답변

1

내가 어떻게하는지. 시도해보기 :

protected void Application_Error(object sender, EventArgs e) 
{       
    //Retrieving the last server error 
    var exception = Server.GetLastError();  

    //Erases any buffered HTML output 
    Response.Clear(); 

    //Declare the exception 
    var httpException = exception as HttpException; 

    var routeData = new RouteData(); 
    routeData.Values.Add("controller", "Error"); //Adding a reference to the error controller 

    if (httpException == null) 
    { 
     routeData.Values.Add("action", "ServerError"); //Non HTTP related error handling 
    } 
    else //It's an Http Exception, Let's handle it. 
    { 
     switch (httpException.GetHttpCode()) 
     { 
      //these are special views to handle each error 
      case 401: 
      case 403: 
       //Forbidden page. 
       routeData.Values.Add("action", "Forbidden"); 
       break; 
      case 404: 
       //Page not found. 
       routeData.Values.Add("action", "NotFound"); 
       break;  
      case 500: 
       routeData.Values.Add("action", "ServerError"); 
       break; 
      default: 
       routeData.Values.Add("action", "Index"); 
       break; 
     } 
    } 

    //Pass exception details to the target error View. 
    routeData.Values.Add("message", exception); 

    //Clear the error on server. 
    Server.ClearError(); 

    //Avoid IIS7 getting in the middle 
    Response.TrySkipIisCustomErrors = true; 

    // Call target Controller and pass the routeData. 
    IController errorController = new ErrorController(); 
    errorController.Execute(new RequestContext(
     new HttpContextWrapper(Context), routeData)); 
}   
+0

오후에 Response.TrySkipIisCustomErrors = true;가 발견 될 때까지 버그를 알아 냈습니다. – Graham

+0

답변 해 주셔서 감사합니다. Ahmed! ServerError 액션에서 예외가 발생하면 어떻게됩니까? – Joe

+0

그레이엄에게 : 나는 나에게도 일어났다. :) – Joe

관련 문제