简体   繁体   English

如何在ASP.NET Core WebApi中将键值对传递给HttpGet?

[英]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. 我正在创建一个WebApi,我需要为GET端点获取一个键值对。 I found some examples of using dictionary in POST method bus this seems not to work with GET 我发现在POST方法总线中使用字典的一些示例似乎不适用于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"}]} ) 我正在使用swagger测试API,如果我通过{"key":"value"}得到的字典只有一对,而value是我传入的整个对象。( {[myVar, {"key":"value"}]}

What is the correct way to pass multiple key value pairs to the WebApi for GET method? 将多个键值对传递给WebApi for GET方法的正确方法是什么?

EDIT: The underlying issue was that I was using swagger (swashbuckle) to test my endpoint. 编辑:潜在的问题是我正在使用摇摇欲坠(swashbuckle)测试我的端点。 And at the moment of this question it doesn't support dynamic query parameters Issue on github . 目前,该问题不支持github上的动态查询参数Issue It should support it once OpenApi v3 support is added to swashbucle Issue on github . 一旦将OpenApi v3支持添加到github上的swashbucle Issue中,它就应该支持它。

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. 您应该能够使用以下结构来调用端点,并通过Web API的内置绑定器自动绑定值。

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

1 and 2=keys for respective entries in dictionaries. 1和2 =字典中各个条目的键。 john and jane=values 约翰和简=值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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