2

Problem is, I have an API that concatenates pdfs from Urls, and it's working in .NET 5 , but when starting to migrate to .NET 6, the use of IEnumerable<> , IFormFile, and IFormFileCollection , simply only accepts requests application/json.

Here is the endpoint in .NET 5 (Working)

[HttpPost]
        public async Task<IActionResult> ConcatenarPdfsByUrl([FromForm] IEnumerable<string> urls)
        {
            var output = await TransformaPdfCore.PdfConcatenation(urls);
            return File(output, "application/octet-stream");
        }

result: Imagem 1 (.net 5)

And so is Endpoint in Minimal .net6

app.MapPost("/ConcatenaPdfsByUrl", async Task<IResult> (IEnumerable<string> urls, TransformaPdfCore transforma) =>
{
    {
        var output = await transforma.PdfConcatenation(urls);
        return Results.File(output, "application/octet-stream");
    }
}).Accepts<IEnumerable<string>>("multipart/form-data");

But the result is this: Imagem 2 (.net 6)

The question is, why does IEnumerable not have the same behavior? and if there is any solution, for example using IOperationFilter, so that I can make it work.

The IFormFileCollection Interface had the same problem

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Gean Souza
  • 21
  • 1

1 Answers1

1

Currently binding from form is not supported for minimal APIs. You can track this issue on github - the support is investigated for .NET 7. You can try to implement custom binding from form via BindAsync method using HttpContext.Request.Form or just add HttpContext parameter to your handler method and use it there:

app.MapPost("/ConcatenaPdfsByUrl", async (HttpContext context, TransformaPdfCore transforma) =>
{
    // use context.Request.Form or context.Request.Form.Files
    var output = await transforma.PdfConcatenation(urls);
    return Results.File(output, "application/octet-stream");
})
Guru Stron
  • 102,774
  • 10
  • 95
  • 132