UPDATE
My original assumption was that optional parameters were the cause of the problem. That appears to be incorrect. Instead, it appears to be a problem with multiple action methods when one of those methods contains nullable value types (e.g. int? ) for some of the parameters.
I'm using Visual Studio 2012 RC and am just getting started with Web API. I've run into an issue and getting the error "No action was found on the controller 'Bars' that matches the request."
I've got a Bars controller. It has a Get() method that takes in optional parameters.
public IEnumerable<string> Get(string h, string w = "defaultWorld", int? z=null)
{
if (z != 0)
return new string[] { h, w, "this is z: " + z.ToString() };
else
return new string[] { h, w };
}
So, I test it out with the following urls
- /api/bars?h=hello
- /api/bars?h=hello&w=world
- /api/bars?h=hello&w=world&z=15
And it works for all three.
Then, I go to add another Get() method, this time with a single id parameter
public string Get(int id)
{
return "value";
}
I test the urls again. This time /api/bars?h=hello&w=world and api/bars?h=hello fail. The error message is "No action was found on the controller 'Bar' that matches the request."
For some reason, these two methods don't play nicely together. If I remove Get(int id)
, it works. If I change int? z to string z, then it works (, but then it requires converting the objects inside my action method!).
Why is Web API doing this? Is this a bug or by design?
Many thanks.