Search code examples
c#asp.net-web-apiasp.net-web-api-routing

How do I use ASP.NET Web API Attribute Routing with a complex object parameter?


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")]