.Net Core WebAPI,无法使用 POSTMAN 发布数据,错误 - 415 Unsupported MediaType [英] .Net Core WebAPI , Unable to post data with POSTMAN , error - 415 Unsupported MediaType

查看:30
本文介绍了.Net Core WebAPI,无法使用 POSTMAN 发布数据,错误 - 415 Unsupported MediaType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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:

这篇关于.Net Core WebAPI,无法使用 POSTMAN 发布数据,错误 - 415 Unsupported MediaType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆