Skip to content

multipart/form-data request forces form read (request body read) before endpoint handler code #62117

Closed
@DeagleGross

Description

@DeagleGross

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

While working on #58233 I tested a scenario with controller-based endpoint handler which has CancellationToken parameter and handles the big file upload:

// curl -X POST -F "file=@D:\.other\big-files\bigfile.dat" http://localhost:5000/file/upload
[HttpPost]
[Route("upload")]
public async Task<IActionResult> UploadCts(CancellationToken cancellationToken)
{
   ...
}

I spotted that because of having any parameter forces model binding, and it eventually calls FormValueProviderFactory.AddValueProviderAsync.

Such behavior means that user cannot for example stream the request body to file stream (if that is the intention).
Accessing and reading from HttpRequest.BodyReader clearly shows that body is already read.

Even for parameter like [FromQuery] string id the same behavior exists:

    // curl -X POST -F "file=@D:\.other\big-files\bigfile.dat" http://localhost:5000/file/upload
    [HttpPost]
    [Route("upload/{id}")]
    public async Task<IActionResult> UploadQueryParam([FromQuery] string id)
    {
        return Ok($"Query completed: query id = {id}");
    }

Expected Behavior

I think aspnetcore should not read the body of the request allowing user to control when that should be happening.

Steps To Reproduce

https://github.com/DeagleGross/aspnetcore/tree/dmkorolev/file-upload-samples/src/Http/samples/FileManagerSample

Exceptions (if any)

No response

.NET Version

net9.0

Anything else?

No response

Metadata

Metadata

Assignees

Labels

✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions