首页 > 解决方案 > 一个只有一个元素的数组来到控制器

问题描述

我尝试在我的控制器中处理 HttpPost 请求。

[HttpPost]
    [Route("device/configuration/config")]        
    public ActionResult<string> config([FromForm, Required] string class, [FromForm, Required] int account_id, [FromForm] string[] config, [FromForm] string creator_name)

如您所见,我想获取配置数组。在我的测试中,我向这个端点发出请求,并以“config:”config1”、“config:”config2”的形式传递数组。一切正常。但是当我从 swagger 中调用它时,我得到的不是一个配置数组,而是一个包含一个项目的数组,其中所有配置都用逗号分隔。

大摇大摆的卷曲

curl -X POST "localhost:7980/device/configuration/account_config" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "device_class=someClass" -F "account_id=2" -F "config="123","321","222"" -F "creator_name=someCreator"

标签: c#asp.net-core

解决方案


你可以这样尝试吗:

curl -X POST "localhost:7980/device/configuration/account_config" -H "accept: text/plain" -H "Content-Type: multipart/form-data" -F "device_class=someClass" -F "account_id=2" -F "config=123" -F "config=321" -F "config=222" -F "creator_name=someCreator"

问题是您必须发送数组参数 3 次。

你也可以这样发送:

curl -X POST "localhost:7980/device/configuration/account_config?device_class=someClass&config=123&config=321&config=222&creator_name=someCreator" -H "accept: text/plain" -H "Content-Type: multipart/form-data"

推荐阅读