MVC - filterContext.ExceptionHandled

Mik*_*eel 7 asp.net-mvc

filterCtext.ExceptionHandled属性是否由MVC设置为true或者是否仅由用户代码设置为true?如果是这样,这会发生在哪里?

Lin*_*Lin 5

filterContext.ExceptionHandled当action方法抛出异常时,get设置为true.默认情况下HandleErrorAttribute已在FilterConfig注册的类中添加Application_Start().发生异常时,将OnExceptionHandleErrorAttribute类中调用该方法.

OnException方法中,在使用之前删除当前HTTP响应主体之前 Response.Clear(),该ExceptionHandled 属性将设置为true.

下面是默认的OnException方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 你有一个"filterContext.ExceptionHandled获取动作方法抛出异常时设置为true"的来源吗? (3认同)