MVC - filterContext.ExceptionHandled
asp.net-mvc
Solution
`filterContext.ExceptionHandled` get set to true when an exception is thrown by an action method. By default `HandleErrorAttribute` has been added in `FilterConfig` class which is registered in `Application_Start()`. When an exception occurs, the `OnException` method is called in `HandleErrorAttribute` class.
In `OnException` method, before removing the current HTTP response body by using `Response.Clear()`, the `ExceptionHandled` property will set to true.
Below is the default OnException method:
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction)
{
return;
}
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}
Exception exception = filterContext.Exception;
if (new HttpException(null, exception).GetHttpCode() != 500)
{
return;
}
if (!ExceptionType.IsInstanceOfType(exception))
{
return;
}
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
Problem
Does the filterContext.ExceptionHandled property ever get set to true by MVC or is it only set to true by user code? If so where does this happen?