In Web API I had a class of similar structure:
public class SomeController : ApiController
{
[WebGet(UriTemplate = "{itemSource}/Items")]
public SomeValue GetItems(CustomParam parameter) { ... }
[WebGet(UriTemplate = "{itemSource}/Items/{parent}")]
public SomeValue GetChildItems(CustomParam parameter, SomeObject parent) { ... }
}
Since we could map individual methods, it was very simple to get the right request at the right place. For similar class which had only a single GET
method but also had an Object
parameter, I successfully used IActionValueBinder
. However, in the case described above I get the following error:
Multiple actions were found that match the request:
SomeValue GetItems(CustomParam parameter) on type SomeType
SomeValue GetChildItems(CustomParam parameter, SomeObject parent) on type SomeType
I am trying to approach this problem by overriding the ExecuteAsync
method of ApiController
but with no luck so far. Any advice on this issue?
Edit: I forgot to mention that now I am trying to move this code on ASP.NET Web API which has a different approach to routing. The question is, how do I make the code work on ASP.NET Web API?
Best Answer
This is the best way I have found to support extra GET methods and support the normal REST methods as well. Add the following routes to your WebApiConfig:
I verified this solution with the test class below. I was able to successfully hit each method in my controller below:
I verified that it supports the following requests:
Note That if your extra GET actions do not begin with 'Get' you may want to add an HttpGet attribute to the method.