c# - 只有一个元素的数组到达 Controller

标签 c# asp.net-core

我尝试在我的 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/

相关文章:

c# - ASP.NET 'FindByNameAsync' 返回空值?

c# - 为什么在采用对象初始化器时使用 (int?) null?

error-handling - 防止直接从请求执行错误页面操作

angular - ASP.NET Core 3 - Angular SPA PWA

c# - Asp.Net Core 是否跨请求保留 CallContext?

nginx - .Net 核心 X-Forwarded-Proto header 无法正确传递给 Nginx

asp.net-core - 我应该在每个页面上放置一个脚本部分,还是在 Layout.cshtml 部分文件中?

c# - `WordprocessingDocument.Open` 后损坏的文件

c# - 在 MVVM 中调用控制方法

c# - 仅在应用程序启动时使用 DI 容器?