Skip to content

Support binding request body as Stream in Controller Actions #41540

@brunolins16

Description

@brunolins16

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

The #38153 introduced the support for binding Stream/PipeReader, since it is common pattern a similar support must be available in Controller Actions.

Describe the solution you'd like

I would like to replicate the same code I have for a Minimal APIs:

app.MapPost("v1/feeds", async (QueueClient queueClient, Stream body, CancellationToken cancellationToken) =>
{
    await queueClient.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
    await queueClient.SendMessageAsync(await BinaryData.FromStreamAsync(body), cancellationToken: cancellationToken);
});

I my Controller Actions:

[Route("v1/feeds")]
[ApiController]
public class FeedsController : ControllerBase
{
    [HttpPost]
    public async Task<ActionResult> Post(QueueClient queueClient, Stream body, CancellationToken cancellationToken)
    {
        await queueClient.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
        await queueClient.SendMessageAsync(await BinaryData.FromStreamAsync(body), cancellationToken: cancellationToken);
        return Created("[somelocation]", null);
    }
}

I should also be able to explicitly define the parameter [FromBody] Stream body and must have the same behavior.

Additional context

The same considerations applied to Minimal should be applied here as well:

  • When ingesting data, the Stream will be the same object as HttpRequest.Body.
  • The request body isn’t buffered by default. After the body is read, it’s not rewindable (you can’t read the stream multiple times).
  • The Stream/PipeReader are not usable outside of the controller action handler as the underlying buffers will be disposed and/or reused.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-model-binding

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions