Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 一次例外-两次呼叫_.net_Asp.net Mvc - Fatal编程技术网

.net 一次例外-两次呼叫

.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

我有一个带有OneException处理程序的BaseController类

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。很简单:)谢谢。如果你把它作为答案贴出来,我会接受的。