The solution here was that the {query}
token in the Route definition was superfluous. Removing it, as follows, fixed the issue:
[Route("api/query/hello")]
Question
I have a Web API action that looks like the following:
[HttpGet]
[Route("api/query/hello/{query}")]
public HttpResponseMessage Hello([FromUri]Query query)
{
return null;
}
where the Query class has a public string property named QueryText
. When I hit the following URL, I get a 404 error:
/api/query/hello?QueryText=bacon
This worked before I started using Attribute Routing. If I have no parameters or primitive type parameters, I can get Attribute Routing to work. But with a complex parameter, I get 404s. How does Attribute Routing work with complex action parameters? Is it compatible with the FromUri
attribute?
Solution
The solution here was that the {query}
token in the Route definition was superfluous. Removing it, as follows, fixed the issue:
[Route("api/query/hello")]
OTHER TIPS
The [FromUri] attribute will be needed because you're reading from the URL. Your route should look something like:
public HttpResponseMessage Hello([FromUri]Query query)
{
//Implement whatever
return null;
}
/api/{Controller Name}/hello?QueryText=bacon
Should then work correctly.
The model binder will take whatever query parameters you provided then try to bind whatever is inside that Query
object. I'd worry about the Route Attribute after you've got it working first.