Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 623
user2719100 Avatar asked Jan 17 '14 21:01

user2719100


1 Answers

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")]
like image 146
user2719100 Avatar answered Sep 21 '22 12:09

user2719100