MVC2 Handling Exceptions from RenderAction
My controller is decorated with [HandleError]
and [HandleError(ExceptionType=typeof(CustomException), View="CustomView")]
. I have views that consist of multiple partial views from multiple controllers, so we're displaying our partial views using <% RenderAction(...) %>
but now we have a case where our action being rendered is throwing a CustomException. I overrode OnException
inside HandleErrorAttribute
which gets called (twice for some reason) with the CustomException
, and then two more times with System.Web.HttpUnhandledException
, before being redirected to the default Error.aspx
page instead of to CustomView
where it should go with this error.
Also, if I remove [HandleError]
but keep [HandleError(ExceptionType=typeof(CustomException), View="CustomView")]
the HandleErrorAttribute.OnException
开发者_如何学编程 override never fires.
How do I throw CustomException types from an action that renders a partial view, and still have the app redirect to my CustomView?
I'm not sure why the exception is so deep, but I got it to work adding this code into OnException
inside HandleErrorAttribute
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";
}
精彩评论