gpt4 book ai didi

c# - 发布 IFormFile 时出现错误请求 (400)

转载 作者:行者123 更新时间:2023-12-02 14:54:37 25 4
gpt4 key购买 nike

我有一个 .NET Core API 项目,它有一个简单的端点来上传文件:

[Route("api/[controller]")]
[ApiController]
public class FilesController : Controller
{
private IFilesService _filesService { get; set; }

public FilesController(IFilesService filesService)
{
_filesService = filesService;
}

[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file)
{
var model = await _filesService.UploadFile(file);
return Ok();
}
}

我尝试使用 Postman 对此进行测试,但每次我将文件发布到端点时,我都会收到 400 Bad Request 错误。我的端点永远不会被击中。

我还有其他几个工作正常的 POST 端点,所以这是这个特定端点或 Postman 的问题。这是我在 Postman 中的设置:

enter image description here

我一直在转动轮子试图找出问题所在,但根本没有什么,我正在按照我在网上看到的例子进行操作。

我做错了什么?

最佳答案

我看到您设置了 [ApiController] 属性,所以我假设您正在运行 ASP.Net Core 2.1。

如果尚未设置,请尝试将 Startup.cs 中的 services.AddMvc() 修改为 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

我前段时间遇到过类似的问题,上面的更改为我修复了它。我找到了问题的答案 here .

希望这对您有所帮助!

关于c# - 发布 IFormFile 时出现错误请求 (400),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com