How to skip action execution from an ActionFilter?

user49126 picture user49126 · Mar 23, 2012 · Viewed 36.7k times · Source

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

Answer

tpeczek picture tpeczek · Mar 23, 2012

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);
}