I develop WebAPI application and I need to use a custom controller selector, so I have created this one:
public class CustomControllerSelector : DefaultHttpControllerSelector
{
(...)
public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
// It's just a simplified logic which exemplify the problem
var controller =_controllerAssembly.GetType("App.Controllers.MyController");
return new HttpControllerDescriptor(this.httpConfiguration, controller.Name, controller);
}
}
My controller contains two GET methods:
public class MyController : System.Web.Http.ApiController
{
[Route("~/api/myController/GetAll")]
public IEnumerable<MyModel> GetAll(){ ... }
public MyModel Get(){ ... }
}
Unfortunately, when I go to http://localhost/api/myController/, I get the error:
Multiple actions were found that match the request:
GetAll on type App.Controllers.MyController
Get on type App.Controllers.MyController"
But if I go to http://localhost/api/myController/GetAll, it works as intended and collection of object is returned.
It is worth mentioning that if I don't use the custom controller selector, it works like a charm and both actions gives valid response.
Do you know why custom controller selector (derived from default selector) causes this problem?
Workaround:
Since the original (default) controller selector is initialized properly and works as expected, I aggregate it instead of inherit from it.
class CustomControllerSelector : IHttpControllerSelector
{
private DefaultHttpControllerSelector defaultSelector;
public CustomControllerSelector(DefaultHttpControllerSelector defaultSelector)
{
(...)
}