How to skip action execution from an ActionFilter?

前端 未结 4 584
轮回少年
轮回少年 2020-12-09 01:10

Is it possible to skip the whole action method execution and return a specific ActionResult when a certain condition is met in OnActionExecuting?

4条回答
  •  情深已故
    2020-12-09 01:35

    You can use filterContext.Result for this. It should look like this:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //Check your condition here
        if (true)
        {
            //Create your result
            filterContext.Result = new EmptyResult();
        }
        else
            base.OnActionExecuting(filterContext);
    }
    

提交回复
热议问题