Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Mvc.Controller Initialize

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.

like image 667
ETFairfax Avatar asked Nov 05 '09 09:11

ETFairfax


People also ask

What is the function for controller in MVC?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

What is ASP Net mvc6?

ASP.NET MVC is a web application framework developed by Microsoft that implements the model–view–controller (MVC) pattern. It is no longer in active development. It is open-source software, apart from the ASP.NET Web Forms component, which is proprietary.


1 Answers

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);
    }
like image 125
Misha N. Avatar answered Oct 01 '22 18:10

Misha N.