2011-10-12 37 views
0

我想在重寫方法OnException中處理異常,如下所示: if i filterContext.ExceptionHandled = true; 返回視圖將顯示空白視圖。 我不希望它重定向到500頁。 那我該怎麼辦?我怎樣才能捕捉到MVC3異常並且不返回空白視圖?

protected override void OnException(ExceptionContext filterContext) 
{ 
    base.OnException(filterContext); 

    var exception = new HttpException(null, filterContext.Exception); 
    if (exception.GetHttpCode() == (int)HttpStatusCode.InternalServerError) 
    { 
     if (WebConstants.systemHandleExceptionList.Contains(filterContext.Exception.GetType())) 
     { 
      //process the error 
      SaveErrorMessage(filterContext.Exception.GetType().Name); 

      filterContext.ExceptionHandled = true; 
     } 
     else 
     { 
      //record the unhandle message to log 
      errorlog.Error("", filterContext.Exception); 
     } 
    } 
} 
+0

你想做什麼? –

回答

0

將標誌設置爲true後,您可以將其重定向到其他頁面。看下面的例子

RouteValueDictionary errorRouteValues = new RouteValueDictionary(new { controller = "Error", action = "Action" }); 

filterContext.Result = new ActionRedirectResult(errorRouteValues); 
filterContext.Cancel = true; 
+0

ActionRedirectResult:它不能在MVC3中使用嗎? – user928359

+0

我不想重定向到其他頁面,我只是想讓它回來並獲得paga上的錯誤顯示。 – user928359

+0

我遇到了同樣的問題:我希望保留在頁面中,並使用Javascript和/或jQueryUI顯示錯誤。 –