Ramūnas
Ramūnas

Reputation: 1634

How to pass key values pairs to HttpGet in ASP.NET Core WebApi?

I am creating a WebApi and I need to take a key value pairs for my GET endpoint. I found some examples of using dictionary in POST method bus this seems not to work with GET

So far I tried this:

[HttpGet]
public IActionResult Get([FromQuery] Dictionary<string, string> myVar)
{
}

I am using swagger to test the API and if I pass {"key":"value"} I am getting my dictionary with a single pair and value is the entire object I pass in. ({[myVar, {"key":"value"}]})

What is the correct way to pass multiple key value pairs to the WebApi for GET method?

EDIT: The underlying issue was that I was using swagger (swashbuckle) to test my endpoint. And at the moment of this question it doesn't support dynamic query parameters Issue on github. It should support it once OpenApi v3 support is added to swashbucle Issue on github.

Upvotes: 4

Views: 3683

Answers (1)

user1011627
user1011627

Reputation: 1811

You should be able to call the endpoint using the following structure and have the values automatically bound via Web API's built-in binder.

https://example.com/api/values?1=john&2=jane

1 and 2=keys for respective entries in dictionaries. john and jane=values

Upvotes: 5

Related Questions