I have the following example where the request is http://{domain}/api/foo/{username}
but I get a 404 status code back. No other Get actions exist on this controller. Shouldn't this work?
public class FooController : ApiController
{
public Foo Get(string username)
{
return _service.Get<Foo>(username);
}
}
By default your route will look something like this:
When you visit the url
http://{domain}/api/foo/{username}
the controller is mapped asfoo
and the optionalid
parameter is mapped to{username}
. As you don't have a Get action method with a parameter calledid
a 404 is returned.To fix this you can either call the API method by changing the URL to be explicit about the parameter name:
Or you could change your parameter name in your action method:
Or you could change your route to accept a
username
: