Bryan
Bryan

Reputation: 5471

Web API GET with array of complex type

I want to allow users of an API to send GET request that will run multiple queries on db and then return the result.

I have a query model like this

public class QueryModel 
{
   public int A {get;set;}
   public int B {get;set;}
   public int C {get;set;} 
}

I have a controller with a Get method like this -

public async Task<IActionResult> Get(List<QueryModel> queryModels)
{
   foreach(var queryModel in queryModels)
   {
      // some logic to search the db
   }
   // combine the results and return

}

This is not working for me, but I don't know if my query string is wrong or the method is wrong.

I have tried a number of variations of

?model={[{1,2,3},{1,2,3}]}

But they do not work.

Upvotes: 0

Views: 330

Answers (1)

user4864425
user4864425

Reputation:

You can use a construction like this:

\Get?model[0].A=1&model[0].B=2&model[0].C=3&model[1].A=4&model[1].B=5&model[1].C=6

Almost forgot, add FromUri:

public async Task<IActionResult> Get([FromUri] List<QueryModel> model)
{
 ...
}

Please let me know if this works for you.

Upvotes: 1

Related Questions