gpt4 book ai didi

asp.net-mvc - MVC - filterContext.ExceptionHandled

转载 作者:行者123 更新时间:2023-12-04 12:21:18 24 4
gpt4 key购买 nike

filterContext.ExceptionHandled 属性是否曾经被 MVC 设置为 true 或者它是否仅被用户代码设置为 true?如果是这样,这是在哪里发生的?

最佳答案

filterContext.ExceptionHandled当 action 方法抛出异常时设置为 true。默认 HandleErrorAttribute已添加到 FilterConfigApplication_Start() 中注册的类.发生异常时,OnException方法在 HandleErrorAttribute 中调用类(class)。

OnException方法,在使用 Response.Clear() 删除当前 HTTP 响应正文之前, 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;
}

关于asp.net-mvc - MVC - filterContext.ExceptionHandled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com