I want to create a simple file upload endpoint in ASP.NET Core 6 and thought it would be as easy as described here https://dotnetthoughts.net/handling-file-uploads-in-openapi-with-aspnet-core/.
When I have an endpoint defined like:
app.MapPost("/upload", (IFormFile file) =>
{
//Do something with the file
return Results.Ok();
}).Accepts<IFormFile>("multipart/form-data").Produces(200);
I get a 415 back when I call the endpoint. The message I get back is something like:
Expected a supported JSON media type but got "multipart/form-data; …
Not sure why it expected a supported json when I say that the endpoint should accept multipart/form-data
.
Any ideas or thoughts on what to do here?
4
Answers
Currently out of the box support for binding in Minimal APIs is quite limited. Supported binding sources:
You can either leverage custom binding or use special types handling:
UPD
Since .net-7.0 Minimal APIs should be able to bind
IFormFile
/IFormFileCollection
directly:Just noting here, you can upload a file with any ContentType like the following sample code.
It is also well supported by Swagger UI:
This has been addressed with .NET 7. Support for IFormFile and IFormFileCollection has been added. You should be able to use it in your MediatR pipelines.
Ref: .NET 7 Minimal API Support for File Upload Bindings