避免使用
[FromBody]
,它将指示ModelBinder读取整个负载,然后将其序列化为
MyPoco
.
为了实现你的目标,你可以声明你的行动方法如下:
[HttpPost("[action]")]
public IActionResult Test(MyPoco myPoco,IFormFile myfile){
// now you get the myfile file and the myPoco
}
然后发送具有完整名称的字段:
<form id="createForm" method="post" enctype="multipart/form-data" action="/api/SampleData/Test">
<input name="MyPoco.Name" type="text" bind="@model.Name" />
<input name="MyPoco.Id" type="text" bind="@model.Id" />
<input name="myfile" type="file" />
<button type="submit">submit this form</button>
</form>
演示截图: