Fraz Sundal
Fraz Sundal

Reputation: 10448

Issue in action call after IActionFilter OnActionExecuting function

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.

Upvotes: 0

Views: 683

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

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.

Upvotes: 1

Related Questions