Issue in action call after IActionFilter OnActionExecuting function

651 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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.