mauzilla mauzilla - 1 year ago 249
C# Question

Redirect in OnActionExecuting not working

I'm trying to redirect a visitor which isn't authenticated via OnactionExecuting() so that it covers my entire controller, but it's almost like it's being skipped entirely. Is it possible to redirect from within OnActionExecuting()?

protected override void OnActionExecuting(ActionExecutingContext filterContext)

if (SimpleAuth.isAuth())
RedirectToAction("Login", "Users");

Answer Source

You can't use RedirectToAction inside filters. RedirectToAction is a member of the Controller class. That's why you can call this method inside an action method.

But, if we are talking about filters, then you must set filterContext.Result to a new RedirectToRouteResult:

 filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
                                    controller = "Users",
                                    action = "Login"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download