c# - 一个只有一个元素的数组来到控制器
问题描述
我尝试在我的控制器中处理 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"
解决方案
你可以这样尝试吗:
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"
推荐阅读
- python - 在不使用任何模块的情况下对可以包含“ç”、“ş”等字符的单词进行排序
- amazon-web-services - MX 记录 MAIL FROM 的 AWS SES 多个值
- node.js - 即使在我的 server.js 中包含 app.get() 后,节点并表示错误“无法获取 /”
- javascript - 如何在 CDN 版本中使用 TickMarkType.TimeWithSeconds
- oracle - Oracle 表单和报告文档
- java - 如何使用继承或其他方法正确组合我的 POJO?
- r - Year_CW 到 R 中的日期
- php - 创建页面以更改刀片中表中的数据时出错
- wordpress - 如何向 Wordpress 网站发送 POST 请求并执行一些操作
- ios - UITextField - 以编程方式的底线