2010-10-28 79 views
0

我的控制器裝有[HandleError][HandleError(ExceptionType=typeof(CustomException), View="CustomView")]。我的視圖由多個控制器的多個部分視圖組成,因此我們使用<% RenderAction(...) %>來顯示局部視圖,但現在我們有一個情況,即我們正在渲染的操作拋出一個CustomException。我忽略裏面的OnException,它被CustomException調用(兩次出於某種原因),然後再用System.Web.HttpUnhandledException兩次,然後被重定向到默認的Error.aspx頁面,而不是CustomView,它應該與這個錯誤一起出現。MVC2處理來自RenderAction的異常

此外,如果我刪除[HandleError]但保留[HandleError(ExceptionType=typeof(CustomException), View="CustomView")]覆蓋HandleErrorAttribute.OnException覆蓋不會觸發。

如何從呈現局部視圖的操作中拋出CustomException類型,並將應用程序重定向到我的CustomView?

回答

0

我不知道爲什麼例外是如此之深,但我得到了它的工作將此代碼添加到裏面OnExceptionHandleErrorAttribute

if (filterContext.Exception.InnerException != null && 
      filterContext.Exception.InnerException.InnerException != null && 
      filterContext.Exception.InnerException.InnerException.InnerException != null && 
      filterContext.Exception.InnerException.InnerException.InnerException.GetType() == typeof(CustomException)) 
     { 
      filterContext.Exception = filterContext.Exception.InnerException.InnerException.InnerException; 
      View = "CustomView"; 
     }