-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Support Form binding for Minimal endpoints #39430
Comments
Thanks for contacting us. We're moving this issue to the |
As a reminder this will require CSRF supoprt. |
I strongly belive this should be a priority!! |
This has many uses and isn't what I would call a frivolous request. |
This is waiting on #38630. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
This is a show-stopper for us. We have got objects with plenty of properties (web pages with a long list of fields) in them that too with files (i.e. text, pdf, etc.). So, if there is no support for [FromForm], we have to go back to the controllers which we desperately would like to avoid. @DamianEdwards and @davidfowl please consider adding the in the next release. 🙏🙏🙏 |
What's the API scenario for form posts? |
Isn't this "done" now as of 7.0 preview 7? |
@davidfowl , Thanks for your quick reply. Let's assume there is a product registration page where the product (class) has a bunch of properties, which has got some complex objects in it as well. In addition, each of the complex objects along with the product has a property to receive files (List<IFormFile> Files { get; set; } ). So it would be really helpful to have an option to receive it all via [FromForm] instead of finding everything from the HttpContext and applying some mapping. Mapping from the HttpContext can be tedious, and error-prone, which can easily be avoided with [FromForm], the way it works for the controller. |
Files are supported but we have no intention of supporting forms for APIs. @ak-hasanuzzaman Thanks for that. Can you elaborate more about the client, what sort of client is this and what does that code look like that produced the form and what is making the post and what does that code look like? |
@davidfowl, The client is a react application, where we use axios to post data to the backend. The following is an example where an object and other complex objects in it, they do have the option to attach files. Eventually, we need to construct a big object and post it to the backend. For example, the post method looks like somewhat similar to the one below (contrived example): The point I am trying to make is to have the option to post data in one stroke. If you are not willing to support forms what are the alternatives when it comes to, posting data with everything in one go? If you want me to clarify even more, please let me know. Thanks again. |
Where's the form? I'd like to see the syntax of the form and how it was created (the syntax used for the fields). The thing I'm trying to understand the serialization format of your fields. Are you using the html helpers from mvc? |
FWIW the OAuth and OIDC specs require form-url-encoded requests payloads for all its programmatic endpoints. So there's an example for you. |
I'm actually OK with very simple form binding. I am not ok with lists, dictionaries, and the custom serialization format the MVC currently supports. |
This is a major problem. I have to transmit BOTH files AND parameters in the same request, which is ONLY possible using FormData!Example:
The profile picture file already uses the body so how should i transmit the request parameters? Only FormData can transmit both at the same time! Other methods don't work:
The only option is FORM DATA !I hope you understand the importance now. |
We'll investigate in .NET 8. |
@Sngmng Thanks for bringing it up mate. @davidfowl I have got something similar. I find the minimal API approach incredible for all the good reasons. But to POST other parameters with the file(s) does require [FormData]. To circumvent that issue we are using a handful of controller endpoints and the rest is minimal which was the only option we were left with. It will be absolutely great if we could get away with the controllers completely by having the [FormData]. Thanks. |
From JavaScript for sure, I looked into this last week and I couldn't find a way to do a multi part post with the fetch APIs. So FormData is the only viable approach for forms + metadata it seems. Generally multipart posts supports sending different content types in the same request, but it's not common. |
@davidfowl thanks for considering. The only way to send the file and the simple text fields is a form. I wish there was another way, but there is not. (We have converted every endpoint to minimal APIs except for these - we have to leave as classic controllers...so not a deal breaker, just not clean) |
Hi, just checking if the work being done here will support a list of IFormFiles and lists of other objects as formdata? This seams like quite a standard thing that should be built in by default. My scenario -
As this is a mixture of data and files formdata is the only valid option |
Yes the below works as of 8.0.0-preview.4. Note that binding to complex objects or enumerables of complex objects is not directly supported though, so to populate your Note however that arrays of primitives are supported: |
Thanks @DamianEdwards Is there any docs or articles that demonstrate this?
|
You could also take a look at https://github.com/DamianEdwards/MinimalApis.Extensions for some examples of implementing custom helpers/wrappers for binding. |
I think we should keep the same behavior for minimal api and mvc controller (also see #49437 ) The inconsistence will make people confuse. For example: in mvc controller: But in minimal api, app.MapPost("/MinimalApi/Upload", async (
string testStr,
int testInt,
IFormFileCollection files1,
IFormFileCollection files2,
IFormFile file1,
IFormFile file2) =>
{
await Task.CompletedTask;
return Results.Ok(files1.Count);
});
[HttpPost("Upload")]
public async Task<IActionResult> Upload(
[FromForm] string testStr,
[FromForm] int testInt,
[FromForm] IFormFileCollection files1,
[FromForm] IFormFileCollection files2,
[FromForm] IFormFile file1,
[FromForm] IFormFile file2)
{
return NoContent();
}
MultipartFormDataContent content = new()
{
// file
{ fileContent1, "files1", "Files1_1.txt" },
{ fileContent2, "files1", "Files1_2.txt" },
{ fileContent3, "files2", "Files2_1.txt" },
{ fileContent4, "files2", "Files2_2.txt" },
{ fileContent5, "file1", "File1.txt" },
{ fileContent6, "file2", "File2.txt" },
// payload
{ new StringContent("Hello"), "testStr" },
{ new StringContent("1"), "testInt" },
};
var response = await httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode(); |
Minimal API won’t be implementing model binding so there’s behavior inconsistency already. For this particular issue it might be worth considering a way to make the behavior consistent with MVC (maybe opt in) |
We've shipped support for form-binding in Minimal APIs as of .NET 8. Closing this for now. We'll track follow-up work in other issues. |
The text was updated successfully, but these errors were encountered: