mauzilla mauzilla - 1 month ago 12x
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");


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"