1

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)
{

    base.OnActionExecuting(filterContext);
    if (SimpleAuth.isAuth())
    {
        RedirectToAction("Login", "Users");
    }
}

1 Answer 1

6

You can't use RedirectToAction inside filters. RedirectToAction is a member of the Controller class. That's why you can call this method inside action methods which are the members of the class which inherits from Controller class.

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

 filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
                                new
                                {
                                    controller = "Users",
                                    action = "Login"
                                }));
1
  • Although it is redirecting now, it redirects to / and not /Users/Login
    – mauzilla
    Sep 23, 2016 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.