Search code examples
c#asp.net-mvcasp.net-mvc-3

Execute code before reaching the controller's code


My intention is to create a MasterController that decides what configurations will use depending on the URL parameters.

  1. My first idea was to create a MasterController. All requests to the server would be redirected to this controller and then, this MasterController would decide which controller to use, and redirect to it. The problem would be that this would make too many requests.

  2. My second idea was to call a function in the beginning of each controller that redirects to the correct controller if necessary. The problem would be the same: it would increase the number of requests.

Concrete example: Imagine that I have the followings URLs:

http:\\myapp.com?showController1=true

http:\\myapp.com\abcd?showController1=true

When the server receives the request, I want the final result be the same: show Controller1, independently of the request being to homeController or abcdController.

To make this work, before which controller is reached, I need to decide which controller will be called depending on the URL parameters. But I want to minimize the number of requests made between the server and the browser.

It is possible to implement this without making extra requests?


Solution

  • After the question edition, I see that most probably it's possible to solve this problem simply by specifying the correct routes.

    The routing happens before the controller which will handle the request is chosen an instanced.

    The result of the routing is to choose a controller, an action, and get parameters from the URL. So, specifying the correct routes would allow you to choose the rigth controller in a non-invasive way (which was what the original answer did). But, this would only work more if you specified the parameters as URL segments, instead of query string parameters. If not, fall back to the original answer.

    Original answer

    Although the question isn't very clear, it looks like you want to choose a particular controller depending on the URL. One way of doing so is implementing your own IControllerFactory, and register it as the ASP.NET MVC controller factory, likt this:

    ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
    

    In the implementation of your custom MyControllerFactory, you have to implement this members:

    • CreateController(RequestContext, String): Creates the specified controller by using the specified request context.
    • GetControllerSessionBehavior(RequestContext, String): Gets the controller's session behavior.
    • ReleaseController(IController): Releases the specified controller.

    Particularly, the first method receives the controller's name, and information including the RouteData - which includes parameter information - and the HttpContext, and returns an instanced controller.

    In general it's not a good idea to do this, because MVC stops workin "as expected", and someone which maintains this project later on will have trouble understanding what's going on.