2017-01-11 3 views
7

私は未知のメディアタイプのエラーが発生しているポストマンネットコアWebAPIの、 - 415サポートされていないのMediaType

を私の最初の.NETのコアWebAPIのをテストしています。

私は何が欠けていますか?

This is postman rest client

これは、これはあなたがContent-Typeヘッダを送信していない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); 
} 
+1

をあなたが何を投稿することができます:あなたの最初のスクリーンショットにマウスポインタ近くにあるドロップダウンでJSON (application/json)を選択します。 「ヘッダー」タブ?応答のためにContent-Typeをapplication/json – Dealdiane

答えて

18

である私の投稿オブジェクト

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

です。 Like this

+0

に設定してみてください。しかし、私はその経路でapiを使っていました。 –

0

これは(私はルート内の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); 
    } 
} 

enter image description here

関連する問題