System.Web.Mvc.Controller Initialize

14,220

I think you are overriding wrong method. Try with OnActionExecuting or OnActionExecuted.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      if (something == true)
          filterContext.Result =  RedirectToAction("DoSomething", "Section");
      else
          base.OnActionExecuting(filterContext);
    }
Share:
14,220

Related videos on Youtube

ETFairfax
Author by

ETFairfax

Updated on April 18, 2022

Comments

  • ETFairfax
    ETFairfax about 2 years

    i have the following base controller...

    public class BaseController : Controller
    {
    
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
    
            if (something == true)
                RedirectToAction("DoSomething", "Section");
    
            base.Initialize(requestContext);
    
        }
    
    }
    

    Basically, all my controllers will derive from BaseController, and it will redirect them if a certain value is true. However, this code does not work!!! The call to RedirectToAction is made, but after the Initialize method is finished, it will just move on to the originally called controller.

    Does that make sense??

    Many thanks,

    ETFairfax.

  • ETFairfax
    ETFairfax over 14 years
    Nice one Misha N thanks for the answer. For anyone else that might read this, the answer is correct, but Misha has typed the wrong parameter; it should be a ActionExecutingContext object being passed. Other than that, it's perfect!! Thanks again Misha.
  • Misha N.
    Misha N. over 14 years
    Ups, fixed that. Glad that I could help ETFairfax