2012-10-09 1 views
0

MVC 응용 프로그램에서 Microsoft 예외 처리 응용 프로그램 블록을 사용하여 예외를 처리 할뿐만 아니라 표준 MVC 오류 페이지를 어떻게 표시합니까?예외 처리 응용 프로그램 블록이있는 MVC 오류 페이지

나는

이 예외시 문제보기로 리디렉션 내 web.config 파일을 업데이트했습니다. 그러나 예외는 더 이상 예외 처리 응용 프로그램 블록 (아래 코드)에 의해 처리되지 않습니다. ExceptionPolicy.HandleException (예 : "AllExceptions", out errorToThrow);

예외 처리는 물론 오류보기도 표시 할 수 있습니까? 저는 ELMAH를 사용하고 싶지 않습니다.

답변

1

로깅 및 오류 처리를 위해 Log4net 및 HttpHandler를 사용하십시오. 다음 샘플 코드를 참조하십시오. 그리고 같은 ExceptionHandler 속성 컨트롤러를 장식합니다. ( [ExceptionHandler] public class BillsController : Controller { })

public class ExceptionHandler : HandleErrorAttribute 
{ 
    private static readonly ILog log = LogManager.GetLogger(typeof(ExceptionHandler)); 

    public override void OnException(System.Web.Mvc.ExceptionContext filterContext) 
    { 
     log.Debug("******** ExceptionHandler.OnException() *****************"); 

     base.OnException(filterContext); 
     Exception ex = filterContext.Exception; 
     if (filterContext.Exception is BusinessException) 
     { 
      log.Debug("<<<<Inside On BusinessException....>>>>" + DateTime.Now); 
      //log.Debug("*** Error Getting From **** " + LOGIN_DETAILS.LoginUserName + 
      //   "(UserId = " + LOGIN_DETAILS.LoginUserID + ")" + "Time =" + DateTime.Now); 


      BusinessException _BusinessException = 
       (BusinessException)filterContext.Exception; 

      StringBuilder errormsg = new StringBuilder(); 
      foreach(MessageInfo msg in _BusinessException.ErrorMessageList) 
      { 
       errormsg.AppendLine(msg.LocalizedMessage); 
      } 

      log.Error("<<<<--------BusinessException-------->>>> : Exception Details -----"+ errormsg.ToString()); 
      //filterContext.ExceptionHandled = true; 

      filterContext.ExceptionHandled = true; 
      RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData); 
      string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Account", action = "Logout", message = filterContext.Exception.Message })).VirtualPath; 
      filterContext.HttpContext.Response.Redirect(url, true); 
     } 
     else 
     { 
      log.Error("Exception Details ---- " + 
       "MESSAGE: " + ex.Message + 
       "\nSOURCE: " + ex.Source + 
       "\\Controller: " + filterContext.Controller.ToString() + 
       "\nTARGETSITE: " + ex.TargetSite + 
       "\nSTACKTRACE: " + ex.StackTrace, 
       ex); 

      filterContext.ExceptionHandled = true; 
      RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData); 
      string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "Exception", action = "Default" })).VirtualPath; 
      //string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "exception", action = "Default", message = filterContext.Exception.Message })).VirtualPath; 
      filterContext.HttpContext.Response.Redirect(url, true); 
     } 

    } 
} 
+0

감사 amesh. .HttpContext.Response.Redirect (url, true) 중에 컨트롤러 액션에 모델을 전달할 수있는 방법이 있습니까? – daniele

+0

물론 .. RedirectToAction ("Action", "Controller"new {model = yourmodel})을 시도해 볼 수 있습니다. (새로운 질문은 다른 질문으로 권장됩니다) – amesh

1
사용자 정의 오류를 표시하도록 web.config 파일을 변경

, 오류 페이지를 표시 할 수 있습니다 이런 식으로.

는 예외가 당신에게이 기능을 추가 handeling에 대한 Global.asax에 :

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

    //handle the exception 
} 
+0

middelpat 고맙습니다. 또한 Global.aspx 페이지에서 특정 컨트롤러/동작에 대한 라우팅 코드를 제공 할 수 있습니까? – daniele

+0

당신이 내 노력을 보상 할 수 있다면 예를 들어 upvoting 나는 내가 당신을 도울 수 있는지 보겠습니다. 어쩌면 새로운 질문을하고 좀 더 많은 정보를 제공해야한다. – middelpat

관련 문제