我尝试在我的 Controller 中处理 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“”这种形式传递数组。一切正常。但是当我大摇大摆地调用它时,我得到的不是一组配置,而是一个包含一个项目的数组,其中所有配置都用逗号分隔。
大摇大摆的 curl
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"
最佳答案
你能不能这样试试:
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"
关于c# - 只有一个元素的数组到达 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54980893/