415 不支持的媒体类型 asp.net 核心

415 Unsupported Media Type asp.net core

详情

我正在尝试 post 从 Postman 到我创建的端点的文件。但它给了我这个错误。我没有在 postman

中传递 header Content-Type

415 Unsupported Media Type

API

[Consumes("multipart/form-data")]
[HttpPost]
public async Task<IActionResult> SendEmail([FromBody]Entity entity)
{
    try
    {

        return OK();
    }
    catch (Exception e)
    {
        throw e;
    }
}

Class

public class Entity 
{
    public List<IFormFile> Files { get; set; }
}

如果您要发布表单数据,请尝试使用 [FromForm] 而不是 [FromBody] 作为方法参数。

在 Postman 中,创建 POST 请求时,默认为 "Text"。将其更改为 JSON.

在 Postman 中,确保您使用的是原始格式及其 JSON 格式。尤其是在发出 GET 请求并且您没有在请求正文中发送任何数据时,请确保请求正文不为空。它必须有一个空对象:{ }