My Stack Overfloweth
My Stack Overfloweth

Reputation: 4944

Implementing Custom Controller Factory involving multiple Assemblies

Currently, we are trying to implement a custom Controller Factory in our API to determine the correct controller to be used depending on an identifier token that is passed in to our API. The way this project is setup is that each different company (there are only about 5) has its own assembly which contains custom Controllers that has the Action Methods needed to execute whatever action someone is attempting to do. One of the requirements I've been sent is that the controllers must be named the same in these assemblies. So you could have, for example, four different Controllers each in a different assembly all named CustomerController. These four controllers all contain Action Methods named the same as well but the implementation within them is completely different.

When using our custom Controller Factory, we're using Reflection to pull and create an instance of the correct controller type based off of which Assembly should be targeted. I've debugged this and confirmed that the correct Controller is being returned as a result.

The issue occurring happens during the Action Method call. Even though our Controller Factory is returning the correct controller for our request, it seems that MVC has possibly pre-loaded every controller from these assemblies and is still looking at these as a possible direction. The exact error we're getting on the Action Method getting called is: The current request is ambiguous between the following action methods

Is there any way to tell MVC to ignore the other controllers that were not returned in the request of the controller factory so it will know not to attempt to look at the other controllers that do not seem to be loaded? What is the point of a ControllerFactory if MVC is just going to check all the other controllers for an Action Method match? There must be something I'm missing here.

The only other solution I've found to work around this is adding a ActionMethodSelector attribute to all of the Action Methods involved. However, this makes the solution pretty brittle when looking forward and considering that every single Action Method would need to have the same attribute repeated across them.

Looking forward to any advice or knowledge on why MVC is behaving this way when I have implemented a custom Controller Factory that should be deciding which Controller is chosen for using an Action Method. Thanks in advance!

Upvotes: 4

Views: 671

Answers (1)

My Stack Overfloweth
My Stack Overfloweth

Reputation: 4944

We found the solution. The Controller Factory was working as intended. The issue was actually in our routing. Before implementing this solution, we had tagged our Action Methods with the Route attribute. So, for example, for every Controller in the different assemblies we had

[Route("Customer/Lookup/{name}")] public ActionResult LookupByName(string name) { // Custom code content here }

It turns out that specifying a Route attribute on these ActionMethods takes precedence over the chosen controller and all controllers that have a match with the currently requested controller/action are called anyway. Our solution was to remove this attribute and put what was needed in the RouteConfig.cs file so it is loaded dynamically and full control is returned to our custom Controller Factory to decide which Controller specifically to use for the chosen ActionMethod.

routes.MapRoute( name: "CustomerLookup", url: "customer/lookup/{name}", defaults: new { controller = "Customer", action = "LookupByName", name = UrlParameter.Optional } );

Upvotes: 1

Related Questions