2011-06-12 2 views
46

내 응용 프로그램에서 발생할 수있는 특정 오류를 기반으로 사용자에게 리디렉션을 보내지 않는보기를 반환하려고하는데 오류를 처리하고 로그를 남기고 싶습니다. 내 기본 컨트롤러 내에서 내 Global.asax - Application_Error() 메서드를 전파하는 오류를 싶지 않아이 메서드는 내 응용 프로그램 내부의 다른 오류를 처리 할 수 ​​있습니다. 사용자가 가짜 URL을 입력하면이 문제를 해결할 방법을 찾은 사람이 있습니까?Asp.net mvc 기본 컨트롤러의 OnException 오버라이드 계속 Application_Error

참고 : 나는 몇 가지 문제에 대한 해결 방법이 있었다 나는 내 주석 코드를 떠난,이 또한

... 나는 가능한 핸들에 여러 예외가 편집을 보여줍니다이 모든 것을 무시 OnException에서 나는 RedirectToAction를 발행하는 경우 예상대로 작동하지만, 난 단지

내 기본 컨트롤러 방법은 ...보기없이 리디렉션을 반환하려면 :

protected override void OnException(ExceptionContext filterContext) 
    { 
     //dont interfere if the exception is already handled 
     if (filterContext.ExceptionHandled) 
      return; 

     //let the next request know what went wrong 
     filterContext.Controller.TempData["exception"] = filterContext.Exception; 

     //log exception 
     _logging.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(filterContext.Exception)); 


     //set up redirect to my global error handler 
     //if (filterContext.Exception.GetType() == typeof(NoAccessException)) 
     // filterContext.Result = View(new RouteValueDictionary 
     // (new { area = "", controller = "Error", action = "PublicError" })); 

     //else { 
     //Only return view, no need for redirection 
     filterContext.Result = View(new RouteValueDictionary 
     (new { area = "", controller = "Error", action = "NoAccess" })); 
     //} 
     //advise subsequent exception filters not to interfere and stop 
     // asp.net from showing yellow screen of death 
     filterContext.ExceptionHandled = true; 

     //erase any output already generated 
     filterContext.HttpContext.Response.Clear(); 

     //base.OnException(filterContext); 
    } 

내 응용 프로그램에 나타날 수있는 다른 오류를 처리해야하는이 방법, I 그 전자가 원하지 않아. 위의 패 널 오 류는

protected void Application_Error() 
     { 

      Exception exception = Server.GetLastError(); 
      // Log the exception. 

      var logger = Container.Get<ILoggingService>(); 
      logger.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(exception)); 

      Response.Clear(); 

      HttpException httpException = exception as HttpException; 

      RouteData routeData = new RouteData(); 
      routeData.Values.Add("controller", "Error"); 

      //if (httpException == null) 
      //{ 
      routeData.Values.Add("action", "PublicError"); 
      //} 
      //else //It's an Http Exception, Let's handle it. 
      //{ 
      // switch (httpException.GetHttpCode()) 
      // { 
      //  case 404: 
      //   // Page not found. 
      //   routeData.Values.Add("action", "HttpError404"); 
      //   break; 
      //  case 500: 
      //   // Server error. 
      //   routeData.Values.Add("action", "HttpError500"); 
      //   break; 

      //  // Here you can handle Views to other error codes. 
      //  // I choose a General error template 
      //  default: 
      //   routeData.Values.Add("action", "General"); 
      //   break; 
      // } 
      //} 

      // Pass exception details to the target error View. 
      routeData.Values.Add("error", 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)); 
     } 

답변

64

는 다음 일을해야 내함으로써 Application_Error() 안에 처리되는 :

protected override void OnException(ExceptionContext filterContext) 
{ 
    if (filterContext.ExceptionHandled) 
    { 
     return; 
    } 
    filterContext.Result = new ViewResult 
    { 
     ViewName = "~/Views/Shared/Error.aspx" 
    }; 
    filterContext.ExceptionHandled = true; 
} 

또한 예외는이 방법에서 발생되지 않거나 Application_Error로 전파됩니다 있는지 확인하십시오.

+0

URL은 그대로 남아 있었습니까? ??? 그게 내가 위의 방법에 의해 달성하려는 경우 즉, 오류가 발생하면 관련보기가 다시 보내고 URL이 동일하게 유지/URL이 변경되지 않습니다 ... – Haroon

+1

@Haron, 예 절대적으로, 그것은 동일하게 유지됩니다 . Yellow Screen of Death 대신 사용자 정의보기가 렌더링된다는 점을 제외하고는 예외를 유발 한 초기 URL이됩니다. –

+0

감사합니다,이 작품은 내가 원했던대로! : p – Haroon

관련 문제