简体   繁体   中英

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM