-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Improve Model Binding for HTTP Request Body as Stream #41426
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
Comments
@davidfowl Great news! Thanks for clarifying. If there is parity with controller actions, then I think this issue could be wrapped up. From my cursory observation of the code, the changes would be made in |
That sounds right. We don't support this for today MVC? We should probably make sure this is consistent for 7.0. cc @brunolins16 |
@davidfowl I quickly look at how Minimal API supports
Is that expected and we should do exactly the same thing for MVC? |
We need to fix that behavior 😄 |
|
@brunolins16 to be clear, I believe that @davidfowl was saying:
@davidfowl do you want both approaches supported in MVC or just with an explicit |
@commonsensesoftware 👍. The issue that I've created is just for "Explicit model binding should work for [FromBody] Stream body in Minimal APIs" (BTW I will use it as issue title 😂). For Mvc we should be able to track from this current issue or maybe we can create a new of just to make it clear. |
@brunolins16 no problem. You're free to track it with as many different issues as you like. I just want to make sure both Minimal APIs and MVC are covered for parity. Currently (e.g. in .NET 6.0), neither are supported. I opened this issue with the intent that both are/should be supported. 😉 |
For Stream I think implicit support should work as well. |
Triage: Bruno shared the scope of the problem here. Assigning to @brunolins16 to split up the work into new issues here. |
Thanks for contacting us. |
I have split the work and we will use this issue to track all of them: |
@commonsensesoftware Closing this issue since the working for Minimal is completed and we have a follow up issue for Controller. Feel free to reopen this if needed. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
When building a web API, authors occasionally need to support file uploads. There are two possible ways this can be achieved today:
HttpContext.Request.Body
directlyIFormFile
orIFormFileCollection
Option 1 - Process Request Body Directly
This implementation will correctly upload a file; however,
HttpRequest
has special binding semantics and will not modeled by the API Explorer, making it impossible to use with OpenAPI without additional work on the developer's part. Even ifHttpRequest
was explored, it would be incorrect because the intent is to document the HTTP body and not the entire HTTP request.Option 2 - Use IFormFile or IFormFileCollection
This is a common approach which uses
multipart/form-data
as the media type to upload one or more files. As specified in RFC 7578, this approach is intended for HTML forms. When a HTML<form>
sends aPOST
back to the server with its key/value pairs, it needs a way to include other content, such as files, in a distinctly separate way - e.g. multipart.While this approach can work for a web API, it is unnecessary. A web API should not have to use HTML semantics to upload a file. Moreover, this approach requires clients to format request bodies in a particular way thus changing the wire protocol of the API.
The following is a completely valid file upload:
The following is also a valid file upload:
Describe the solution you'd like
Any HTTP request with a body can potentially be considered a file upload. "There can be only one" parameter bound to the HTTP request body.
For traditional, controller actions, an argument defined as
[FromBody] Stream body
, where the namebody
is irrelevant, should be handled byBodyModelBinderProvider
andBodyModelBinder
. The current implementation does not allow zeroInputFormatter
instances, but it should. Binding toStream
should be considered a special case and should be bound when the following are true:ModelBinderProviderContext.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Body)
context.Metadata.ModelType.Equals(typeof(Stream))
InputFormatter
instances need not be considered. The onus of understanding and processing the content is on the developer who makes a conscience decision to use this setup. A developer can specify which file types are allowed by declaratively using[Consumes]
or imperatively asserting the content.For Minimal APIs, a method parameter defined as
Stream body
or[FromBody] Stream body
should be sufficient. A developer can specify which file types are allowed by declaratively usingAccepts
or imperatively asserting the content.Additional context
Related to #4868
There should also be better support
multipart/*
; specifically for file uploads. This feature request has general applicability beyond file uploads so I'll track that as a separate issue.The text was updated successfully, but these errors were encountered: