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.
Asked
Active
Viewed 625 times
1 Answers
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
-
+1 Thanks that works. My mistake is that im not overriding it like base.OnActionExecuting(filterContext); – Fraz Sundal Dec 23 '10 at 08:55
-
If request is ajax based then its not redirecting to that page instead its opening it in the window – Fraz Sundal Jan 06 '11 at 12:39