.Net Core WebAPI , Unable to post data with POSTMAN , error - 415 Unsupported MediaType

本小妞迷上赌 提交于 2019-12-05 00:23:06

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

This worked for me (I was using api in the route)

[Produces("application/json")]
[Route("api/Countries")]
public class CountriesController : Controller
{
    // POST: api/Countries
    [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);
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!