4

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!

4

1 回答 1

1

我们找到了解决方案。控制器工厂按预期工作。问题实际上出在我们的路由中。在实施这个解决方案之前,我们已经用 Route 属性标记了我们的操作方法。因此,例如,对于我们拥有的不同程序集中的每个控制器

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

事实证明,在这些 ActionMethods 上指定 Route 属性优先于所选控制器,并且无论如何都会调用与当前请求的控制器/动作匹配的所有控制器。我们的解决方案是删除此属性并将所需的内容放入 RouteConfig.cs 文件中,以便动态加载并将完全控制权返回给我们的自定义控制器工厂,以决定哪个控制器专门用于所选的 ActionMethod。

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

于 2015-01-20T15:55:28.307 回答