.Net Core WebAPI,无法使用 POSTMAN 发布数据,错误 - 415 Unsupported MediaType错误、数据、WebAPI、Net

2023-09-07 10:45:17 作者:A逼C迪E爱抚鸡

我正在使用 Postman 测试我的第一个 .net Core WebAPI

I am testing my first .net Core WebAPI with Postman

发生未知媒体类型错误.

unknown media type error is occurring.

我错过了什么?

这是我的发帖对象

public class Country
{
    [Key]
    public int CountryID { get; set; }
    public string CountryName { get; set; }
    public string CountryShortName { get; set; }
    public string Description { get; set; }
}

这是 webapi 控制器

This is the webapi controller

[HttpPost]
public async Task<IActionResult> PostCountry([FromBody] Country country)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    _context.Country.Add(country);
    try
    {
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateException)
    {
        if (CountryExists(country.CountryID))
        {
            return new StatusCodeResult(StatusCodes.Status409Conflict);
        }
        else
        {
            throw;
        }
    }

    return CreatedAtAction("GetCountry", new { id = country.CountryID }, country);
}

推荐答案

您没有发送 Content-Type 标头.在第一个屏幕截图中鼠标指针附近的下拉列表中选择 JSON (application/json):

You're not sending the Content-Type header. Choose JSON (application/json) in the dropdown near the mouse pointer on your first screenshot: