2011-10-12 41 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显示错误。 –