0

I am using IActionFilter OnActionExecuting function for some security check, but when it calls it check a condition and if that conditions fails i want to redirect it to login action but the problem is, it call the next action as well which i donot want to execute if that conditions is fails.

Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132

1 Answers1

1

This should do the job:

public class MyCustomActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (someCondition)
        {
            var values = new RouteValueDictionary(new { 
                action = "index",
                controller = "login"
            });
            filterContext.Result = new RedirectToRouteResult(values);
        }
        base.OnActionExecuting(filterContext);
    }
}

Make sure your attribute derives from ActionFilterAttribute.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928