c# - 如何接收带有 application-json 内容类型和边界的正文附件?
问题描述
由于客户端,我们的 API 应该替换其他 API 而无需进行太多更改,但是以前的 API 以某种形式接收数据,而我无法在 .net 核心中复制这些数据。在这种情况下,ModelState 毕竟是无效的。
请参阅客户要求。
我们正在使用 .net core 2.0 mvc
我已经尝试接受 body 作为 IFormFile 但由于 application/json 内容类型而没有成功。我也尝试接受 body 作为 FromForm 属性参数,但也没有成功。
我的控制器:
[Consumes("application/json")]
[HttpPost("product/set")]
public async Task<IActionResult> CreateProduct([FromBody]IEnumerable<CreateProductInput> input)
{
// Some code and returns.
}
创建产品输入:
public class CreateProductInput
{
[Required]
[StringLength(20, ErrorMessage = "{0} cannot exceed {1} characters")]
public string id { get; set; }
[StringLength(20, ErrorMessage = "{0} cannot exceed {1} characters")]
public string name { get; set; }
}
但客户端请求如下所示:
标题:
Content-Type: application/json; charset=UTF-8; boundary=--------------------------xxxxxxxxxxxxxxx
Content-Length: 1931
身体:
--------------------------xxxxxxxxxxxxxxx
Content-Disposition: attachment; name="data_file"
[
{
"id": "A1",
"name": "AAA",
}, {
"id": "A2",
"name": "BBB",
}, {
"id": "A3",
"name": "CCC",
}
]
--------------------------xxxxxxxxxxxxxxx--
是否可以接收这种带有application/json
内容类型的 json 附件以及如何接收?