Search code examples
c#restasp.net-corepostmanpostdata

.NET Core API - Why is file attribute in nested model receives null?


I have a nested model. When I make a post request with Postman I can get all attributes except the nested File attribute. Why do only nested file attribute comes null and what is the solution? The nested model and screenshots of the request are below:

public class RequestDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IFormFile Image { get; set; }
    public TestModel Test { get; set; }
}

public class TestModel
{
    public string Desc { get; set; }
    public IFormFile File { get; set; }
}

enter image description here enter image description here


Solution

  • Use Test.file instead of Test[file].

    curl --location --request POST 'https://localhost:7212/weatherforecast' \
    --form 'id="1212"' \
    --form 'name="qweqweqw"' \
    --form 'image=@"/<Folder-path>/sun-flower-transparent-background-additional-png-file-greeting-cards-holiday-wishes-sun-flower-transparent-129371261.jpg"' \
    --form 'Test[desc]="csdds"' \
    --form 'Test.file=@"/<Folder-path>/Fruit-PNG.png"'
    

    Demo

    enter image description here