Skip to content

IFormFileCollection minimal api behave different from mvc controller #49437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
kzhui125 opened this issue Jul 15, 2023 · 1 comment
Closed
1 task done

IFormFileCollection minimal api behave different from mvc controller #49437

kzhui125 opened this issue Jul 15, 2023 · 1 comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Comments

@kzhui125
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I can get files with minimal api.
I can't get files with mvc controller.

Expected Behavior

No response

Steps To Reproduce

MapPost:

app.MapPost("/MinimalApi/Upload", async (IFormFileCollection files) =>
{
    await Task.CompletedTask;
    return Results.Ok(files.Count);
});

MapController:

[ApiController]
[Route("api/[controller]")]
public class TestController : ControllerBase
{
    // /Test/Upload
    [HttpPost("Upload")]
    public async Task<IActionResult> Upload([FromForm] IFormFileCollection files)
    {
        await Task.CompletedTask;
        return Ok(files.Count);
    }
}

Call await UploadAsync(urlMinimalApi) and await UploadAsync(urlMvcController)

the responseContent of MinimalApi is 2 (expected).
the responseContent of MvcController is 0 (unexpected).

private async Task UploadAsync(string url)
{
    using HttpClient httpClient = new();

    var content = new MultipartFormDataContent();

    using var stream1 = new MemoryStream("Content1"u8.ToArray());
    var fileContent1 = new StreamContent(stream1);
    fileContent1.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
    content.Add(fileContent1, "File1", "File1.txt");

    using var stream2 = new MemoryStream("Content2"u8.ToArray());
    var fileContent2 = new StreamContent(stream2);
    fileContent2.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
    content.Add(fileContent2, "File2", "File2.txt");

    var response = await httpClient.PostAsync(url, content);
    response.EnsureSuccessStatusCode();

    string responseContent = await response.Content.ReadAsStringAsync();

    Console.WriteLine(responseContent);
}

Exceptions (if any)

No response

.NET Version

7.0.304

Anything else?

What's wrong with my code? Or there is a bug in http client?

Thanks for your help.

@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jul 15, 2023
@davidfowl davidfowl added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Jul 16, 2023
@kzhui125
Copy link
Author

Update:

if the HTTP content item name is "files", then the minimal api works the same as mvc controller.

content.Add(fileContent1, "files", "File1.txt");
content.Add(fileContent2, "files", "File2.txt");

It seems that because of minimal api lacks form binding support (#39430), cause the inconsistent behavior with mvc controller.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Projects
None yet
Development

No branches or pull requests

2 participants