ASP.NET Core 2.1中[ApiController]中的POST操作 [英] POST actions in [ApiController] in ASP.NET Core 2.1
问题描述
我有以下ASP.NET Core 2.1 Api控制器:
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()
{ ... }
GET和POST动作均与Postman一起使用.
Both the GET and POST actions worked with Postman.
但是,POST操作不适用于UI:该操作将始终接收文件参数计数0(请参阅
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).
我最终通过从控制器中删除[ApiController]
属性来解决该错误:
I eventually fixed the fault by removing the [ApiController]
attribute from the controller:
[Route("api/[controller]")]
//[ApiController]
public class ImagesController : ControllerBase
{ ... }
我的问题是为什么[ApiController]
控制器属性阻止POST方法从UI接收文件?这是怎么回事是路由问题吗?
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?
推荐答案
您需要设置兼容版本:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
根据 https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.1
这篇关于ASP.NET Core 2.1中[ApiController]中的POST操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!