我在 .net5 api 应用程序中有两个具有一对多关系的模型。子模型包含 IFormFile 属性。我无法绑定这个 属性

I have two models in .net5 api application with one to many relationship. child model contains IFormFile property. I cannot bind this property

这是我的模型

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<CategoryDetail> CategoryDetails { get; set; }
}

public class CategoryDetail
{
    public int Id { get; set; }
    public string Url { get; set; }
    public IFormFile File { get; set; }
    public Category Category { get; set; }
}

我的控制器功能是

    [HttpPost]
    public IActionResult Create([FromForm] Category category)
    {
        throw new NotImplementedException();
    }

当我通过postman传递数据时,controller方法里面的参数总是null

无法将两个不同的内容类型数据传递到后端。

你的键名有误,应该是CategoryDetails[0].File而不是CategoryDetails[0].files。密钥名称应与 属性 名称匹配,并且不区分大小写。

正确的方法应该是这样的:

注:

如果您 post CategoryDetails 只有 File 属性,您需要确保删除 [ApiController][FromForm] 属性。这里是a known github issue。如果您 post CategoryDetails 具有多个属性,则无需删除任何属性。