0

Here's my scenario. I have two controllers with the same name, in different MVC projects. Each controller can contain the same actions (same name), but can also contain different actions (not specified in the other controller). I've registered two routes, each with their own namespace.

When I browse to the page "Controller/Action1", this is OK. When I browse to "Controller/ActionInSecondController" (the action is not in the first controller) it throws a 404.

Is it possible, and how, to be able to call the second controller's action method?

(Note: I added a referenced the second project and its dll is compiled into the same bin folder location).

my route definitions:

routes.MapRoute(
            name: "Default1",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[]{ "Site1.Controllers" }
        );
routes.MapRoute(
                name: "Default2",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[]{ "Site2.Controllers" }
            );
tereško
  • 58,060
  • 25
  • 98
  • 150
  • It would be much easier to decipher if you included your route definition code, and pared down versions of your controller classes. – jmoerdyk Oct 25 '13 at 21:59
  • Two routes in two projects that are the same. It all depends on which project is taking the web request. All requests are going to the first one. – andleer Oct 25 '13 at 22:17
  • Both routes are in Site1's routeconfig, in that order. The project is just a barebones empty project that I'm trying to get a proof of concept for. The behavior I need is for Site2's action to be called if Site1's controller doesn't contain the action. – user2921642 Oct 25 '13 at 22:19
  • It can't be done just with routing. May be it helps: http://stackoverflow.com/questions/401376/asp-net-mvc-put-controllers-into-a-separate-project – Andrey Gubal Oct 26 '13 at 07:03
  • Thanks Andrey.Gubal. That does help in a sense that it confirms the way I need to get it done is through a custom controller factory. – user2921642 Oct 28 '13 at 15:52

1 Answers1

0

Don't think that is possible, since it will always look for action in the first controller/route it finds.

You either need to add extra identifying info: "url: "controller1/{controller}/{action}/{id}"", or possible create another controller that acts as a proxy (extend the controller you want to take preference and add extra missing methods from second controller):

NewController : Site1.Controllers.Controller

Or you can even use reflection and create a controller that does resolving logic for you based on parameters passed.

Woland
  • 2,881
  • 2
  • 20
  • 33
  • I will accept this as the answer because "it is not possible" with the default routing mechanism. The best way to go about getting this done is to create a custom controller factory and return the controller that has the action I need. I don't want to create more routes and redirect etc... – user2921642 Oct 28 '13 at 15:51