2010-10-28 78 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"; 
     }