Skip to content

Support binding request body as Stream in Controller Actions #41540

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

Open
1 task done
brunolins16 opened this issue May 5, 2022 · 1 comment
Open
1 task done

Support binding request body as Stream in Controller Actions #41540

brunolins16 opened this issue May 5, 2022 · 1 comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-model-binding
Milestone

Comments

@brunolins16
Copy link
Member

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.
@brunolins16 brunolins16 added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label May 5, 2022
@BrennanConroy BrennanConroy added this to the .NET 7 Planning milestone May 5, 2022
@ghost
Copy link

ghost commented May 5, 2022

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Jun 20, 2023
@captainsafia captainsafia modified the milestones: .NET 8 Planning, Backlog Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-model-binding
Projects
No open projects
Status: No status
Development

No branches or pull requests

3 participants