How to skip action execution from an ActionFilter?

前端 未结 4 588
轮回少年
轮回少年 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:42

    You can use the following code here.

    public override void OnActionExecuting(ActionExecutingContext filterContext)
     {
        ...
        if (needToRedirect) //your condition here
        {
           ...
           filterContext.Result = new RedirectToAction(string action, string controller)
           return;
        }
        ...
     }
    

    RedirectToAction will redirect you the specific action based on the condition.

提交回复
热议问题