I am uploading a file as a stream with a workaround the Stream
model binding from this issue, and I want to support consumes for multiple MIME types. I assumed this would work, but it doesn't:
public class FileController : BaseController
{
[HttpPost("customer/{customerId}/file", Name = "UploadFile")]
[SwaggerResponse(StatusCodes.Status201Created, typeof(UploadFileResponse))]
[Consumes("application/octet-stream", new string[] { "application/pdf", "image/jpg", "image/jpeg", "image/png", "image/tiff", "image/tif"})]
//[Consumes("application/octet-stream", "application/pdf", "image/jpg", "image/jpeg", "image/png", "image/tiff", "image/tif")] // doesn't work either
public async Task<IActionResult> UploadFile([FromBody] Stream file, [FromRoute] string customerId, [FromQuery] FileQueryParameters queryParameters)
{
// file processing here
}
}
It only supports "application/octet-stream". Any of the others such as "image/jpeg" fail with a 415 Unsupported Media Type.
I am unable to add multiple ConsumeAttributes
. The documentation for ConsumeAttribute.ContentTypes states:
Gets or sets the supported request content types. Used to select an action when there would otherwise be multiple matches.
I have no idea what that documentation is trying to state, but I had assumed it was a way to support extra MIME types! Is there any way around this in order to support multiple MIME types?
Update The method signature here is fixed and can't be changed. The ConsumesAttribute is being used to generate a Swagger JSON file which clients use to generate their own multi-platform clients for this API.
Usually a Web API controller has maximum of five actions - Get(), Get(id), Post(), Put(), and Delete(). However, if required you can have additional actions in the Web API controller.
ASP.NET Core apps should be designed to process many requests simultaneously. Asynchronous APIs allow a small pool of threads to handle thousands of concurrent requests by not waiting on blocking calls. Rather than waiting on a long-running synchronous task to complete, the thread can work on another request.
Your Consumes attribute is correct. I tested it with dotnet core 2.1 and it works as expected:
[HttpPost("test")]
[Consumes("text/plain", new[] { "text/html" })]
public void Test()
{
}
Sending a post request with Content-Type "text/plain" or "text/html" works while other content types are denied with 415 unsupported media type.
BUT: It stops working if I add [FromBody] Stream file.
// Does NOT work:
[Consumes("text/plain", new[] { "text/html" })]
public void Test([FromBody] Stream file)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With