0

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)
    { 
        (...) 
}
9
  • Also http://localhost/api/myController/get will work. Yes? Commented Jan 26, 2018 at 9:01
  • No, it doesn't, because I use default WebAPI routing configuration: ` config.MapHttpAttributeRoutes(); (...) routeTemplate: "api/{controller}/{id}" (...) Commented Jan 26, 2018 at 9:06
  • I'm sorry, but I didn't mention that I had also Get(int id) action. Nevertheless, I remove this method and I get the same result as I described in my post (api/myController/get causes error if I use custom selector, otherwise it works properly). Commented Jan 26, 2018 at 9:19
  • Dose not MyController inherited from ApiController in your project? Commented Jan 26, 2018 at 9:20
  • Actually, it does. I've corrected my post. Commented Jan 26, 2018 at 9:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.