How to redirect from OnActionExecuting in Base Controller?

CloudMeta picture CloudMeta · Jul 9, 2010 · Viewed 96.2k times · Source

I have tried two ways: Response.Redirect() which does nothing, as well as calling a new method inside of the Base Controller that returns an ActionResult and have it return RedirectToAction()... neither of these work.

How can I do a redirect from the OnActionExecuting method?

Answer

womp picture womp · Jul 9, 2010
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
    ...
    if (needToRedirect)
    {
       ...
       filterContext.Result = new RedirectResult(url);
       return;
    }
    ...
 }