.net 一次例外-两次呼叫
我有一个带有OneException处理程序的BaseController类.net 一次例外-两次呼叫,.net,asp.net-mvc,.net,Asp.net Mvc,我有一个带有OneException处理程序的BaseController类 public class ApiBaseController : Controller { protected override void OnException(ExceptionContext filterContext) { filterContext.Result = ... filterContext.HttpContext.Response.StatusCod
public class ApiBaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
filterContext.Result = ...
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
filterContext.ExceptionHandled = true;
}
}
我继承的控制器在其操作上具有自定义HandleJsonError:
public class ApiCompanyController : ApiBaseController
{
[HttpPost, HandleJsonError]
public ActionResult Delete(int id)
{
// ...
if (...) throw new DependentEntitiesExistException(dependentEntities);
// ...
}
}
HandleJsonError是:
public class HandleJsonError : HandleErrorAttribute
{
public override void OnException(ExceptionContext exceptionContext)
{
// ...
exceptionContext.ExceptionHandled = true;
}
}
当DependentEntityExistException异常出现时,将同时调用基本控制器和HandleJsonError的OneException处理程序。HandleJsonError的OneException完成后,我如何使not call base controller OneException不被调用?检查基本控制器是否已处理异常。如果是,请跳过方法执行:
public class ApiBaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
//Do not continue if exception already handled
if (filterContext.ExceptionHandled) return;
//Error handling logic
filterContext.Result = ...
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
filterContext.ExceptionHandled = true;
}
}
附言:新年快乐 您不能在基本控制器中检查异常是否已被处理,例如
if(filterContext.ExceptionHandled)return代码>?@DanielJ.G。很简单:)谢谢。如果你把它作为答案贴出来,我会接受的。