4

I have the following ASP.NET Core 2.1 Api controller:

[Route("api/[controller]")]
[ApiController]
public class ImagesController : ControllerBase
{
    [HttpPost("[action]")]
    public async Task<IActionResult> Upload([FromForm]ICollection<IFormFile> files)
    { ... }

    [HttpGet("thumbnails")]
    public async Task<IActionResult> GetThumbNails()
    { ... }

Both the GET and POST actions worked with Postman.

However, the POST action would not work with the UI: the action would always receive a files parameter count of 0 (see here for full details of the fault).

I eventually fixed the fault by removing the [ApiController] attribute from the controller:

[Route("api/[controller]")]
//[ApiController]
public class ImagesController : ControllerBase
{ ... }

My question is why did the [ApiController] controller attribute prevent the POST method from receiving the files from the UI? What is happening here? Is it a routing problem?

2 Answers 2

3

You need to set the compatibility version:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

According to https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.1

Sign up to request clarification or add additional context in comments.

2 Comments

Aha np :) I had almost the same issue today, so I found your question. Adding this fixed it for me. Maybe it will help other people.
Fixed it for me, but I also add to add the following to the params for a single file upload public async Task<ActionResult> upload([FromForm(Name="uploadedFileName")]IFormFile uploadedFileName)
3

The ApiController Attribute was added in ASP.NET Core 2.1 and includes binding source parameter inference. There is a recent commit to Infer BindingSource.FormFile for IEnumerable which may be related.

3 Comments

I read that part of the guidance when I upgraded my project from Core 2.0 -> Core 2.1. So are you saying it is a fault?
I can't say for sure, but you could try .NET Core 2.1.1 and see if it resolves your issue.
[ApiController] check if the ModelState is Valid (else give you a 400 response)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.