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
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.
I haven't found a true answer for this issue yet (why is Web API doing this), but I have a workaround that does allow for an overloaded Get(). The trick is to wrap the parameter values in an object.
public class Foo
{
public string H { get; set; }
public string W { get; set; }
public int? Z { get; set; }
}
And to the Bars controller modify to
public IEnumerable<string> Get([FromUri] Foo foo)
{
if (foo.Z.HasValue)
return new string[] { foo.H, foo.W, "this is z: " + foo.Z.ToString() };
else
return new string[] { foo.H, foo.W, "z does not have a value" };
}
[FromUri]
is necessary, because WebAPI does not, by default, use URI parameters to form "complex" objects. The general thinking is that complex objects are coming from <form>
actions, not GET requests.
I'm still going keep checking about why Web API behaves this way and if this is actually a bug or intended behavior.
Problem solved, although, it leaves an additional question. The problem appears to be that the overloaded Action methods are having problems with the optional parameters.
So the new question is why so, but I will leave that up to lower level guys than me ;)
But this is good news. I didn't like the problem you reported, and going the complex type route, while nice to know, is simply a jerry rig fix and would reflect very poorly on how something is working in the Web Api. So the good news is, if you have this problem, it is solved by simply doing away with the optional params, do the good ol' overloads route. Good news, as this is by no means a jerry rig fix, simply makes you loose a little optional parameter convenience:
public class BarsController : ApiController
{
public string Get(int id)
{
return "value";
}
public IEnumerable<string> Get(string h)
{
return Get(h, null, null);
}
public IEnumerable<string> Get(string h, string w)
{
return Get(h, w, null);
}
public IEnumerable<string> Get(string h, string w, int? z)
{
if (z != 0)
return new string[] { h, w, "this is z: " + z.ToString() };
else
return new string[] { h, w };
}
}
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With