0

I have an MVC 4 (at the moment anyway) site which I want to offer to a number of customers. I thought I had clever idea and would create Custom controllers and then select which to use based on a parameter or something.

Each customer will have their own installation so the controller to use should be chosen at compile time and not run time ideally.

So the idea was to create a controller per Customer e.g. Customer1Controller, Customer2Controller etc. and then map it so all the customers would use http://myserver/Customer.

I tried mapping all Customer/{action} to Customer1/{action} but I could not find anyway to map all actions in one statement and having one for every action seems a bit ugly.

My first idea was to try and use dependency injection but as the concrete classes are used for controllers that is apparently not an option. It has some other shortcomings as well.

The actual question is; is there anyway to do a "catch all" for actions? Something like this:

        routes.MapRoute(
            name: "Import",
            url: "Customer/*/{id}",
            defaults: new { controller = "Customer1", action = *, id = UrlParameter.Optional }
        );

Thanks./H

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
VikingIT
  • 37
  • 1
  • 7

1 Answers1

2

The actual question is; is there anyway to do a "catch all" for actions?

Yes. The default route has a good example of that.

    routes.MapRoute(
        name: "Import",
        url: "Customer/{action}/{id}",
        defaults: new { controller = "Customer1", id = UrlParameter.Optional }
    );

This builds the route values dictionary with the action name from the URL, so it can be any action on the "Customer1" controller.

That said, your question is unclear. Controllers are dynamic. Unless you have drastically different actions on each customer controller, there is no reason you need to make a controller per customer.

Or, if what you are trying to do is allow the customer to create their own URLs, you can do so by using a custom RouteBase subclass.

The bottom line is there are many ways to customize URLs, but they don't involve injecting "dynamic controllers", they take advantage of routing to dynamically deliver requests to specific controller actions.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Messed up somewhere as I tested that combination and it always routed tot the Index.default action. But it works now so Thank you very much. I have one section of the site that is Customised to the customer. Hence the idea to use different controllers for that part (as I use DI to select the classes for the business logic) but wasn't possible or a good idea thinking about it.Some actions will be different and I want o avoid having a multiple of if statements for the others.. – VikingIT Jun 14 '17 at 13:15