2017-01-11 13 views
7

Tôi đang thử nghiệm WebAPI Lõi .net đầu tiên của tôi với PostmanNet Lõi WebAPI, Không thể gửi dữ liệu với Postman, lỗi - 415 không được hỗ trợ MediaType

chưa biết lỗi loại phương tiện truyền thông đang xảy ra.

Tôi đang thiếu gì?

This is postman rest client

Đây là của tôi đối tượng gửi bài

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

Đây là bộ điều khiển 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

Bạn có thể gửi những gì trong Tab "Tiêu đề"? Hãy thử đặt Content-Type thành application/json – Dealdiane

Trả lời

18

Bạn sẽ không gửi Content-Type tiêu đề. Chọn JSON (application/json) trong menu thả xuống gần con trỏ chuột trên ảnh chụp màn hình đầu tiên của bạn: Like this

+0

cảm ơn câu trả lời, nhưng tôi đã sử dụng api trong tuyến đường. –

0

này đã làm việc cho tôi (tôi đã sử dụng api trong con đường)

[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

Các vấn đề liên quan