Search code examples
c#.netasp.net-corepostmanasp.net-apicontroller

How to post nested lists of IFormFile in .NET Core Web API?


I am working to an ASP.NET Core Web API project (.NET 7.0). I am trying to build an endpoint to send data along with files. The Post controller action accepts an instance of the ParentDto class. This class contains a list of ChildDto, each one having a string value, a collection of IFormFile and an optional list of children of the same type.

public class ParentDto
{
    public int Id { get; set; }
    public List<ChildDto> Children { get; set; }
}

public class ChildDto
{
    public string? Value { get; set; }
    public List<IFormFile>? Files { get; set; }
    public List<ChildDto>? Children { get; set; }
}

[ApiController]
public class TestController : ControllerBase
{
    [HttpPost]
    public IActionResult Post([FromForm] ParentDto dto)
    {
        // Controller code...
    }
}

When I send data with Postman along with files, the Files property is populated only for the first level of the hierarchy. The children[0][children][0].files property is null, while all others are correctly populated. Even if I add more than one child to the ParentDto.Children property, the lists of files are handled correctly for each child. The problem seems to be in the nested list.

Data sent to the Post endpoint


Solution

  • My postman picture like:

    enter image description here

    result:

    enter image description here