.Net Core WebAPI,无法使用 POSTMAN post 数据,错误 - 415 不支持的媒体类型
.Net Core WebAPI , Unable to post data with POSTMAN , error - 415 Unsupported MediaType
我正在使用 Postman 测试我的第一个 .net Core WebAPI
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 控制器
[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
header。在第一个屏幕截图中鼠标指针附近的下拉菜单中选择 JSON (application/json)
:
这对我有用(我在路线中使用 api)
[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);
}
}
我正在使用 Postman 测试我的第一个 .net Core WebAPI
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 控制器
[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
header。在第一个屏幕截图中鼠标指针附近的下拉菜单中选择 JSON (application/json)
:
这对我有用(我在路线中使用 api)
[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);
}
}