将 json 发布到我的 web-api 时出现问题。我该如何解决

Problem with posting json to my web-api. How can i solve it

我是网络新手 api,现在使用 .net 3 制作一个简单的程序 在我的控制器中,我创建了简单的 post 方法

[HttpPost]
public ActionResult Post(CountryPostRequest countryPostRequest)
{
    var savedCity = new City {Name=countryPostRequest.Capital};
    var savedRegion = countryDb.Regions.Add(new Region { Name = countryPostRequest.Region });
    var savedCountry = countryDb.Countries.Add(new Countrie { Name = countryPostRequest.Name, Region = new Region { Name = countryPostRequest.Region }, Capital = savedCity, AreaSize = countryPostRequest.AreaSize, CountryCode = countryPostRequest.CountryCode, Population = countryPostRequest.Population });
    countryDb.SaveChanges();
    return RedirectToAction(nameof(Get));
}

此方法获取 CountryPostRequest。好像是这样

public class CountryPostRequest
{
    public string Name { set; get; }
    public string CountryCode { set; get; }
    public int Population { set; get; }
    public double AreaSize { set; get; }
    public string Capital { set; get; }
    public string Region { set; get; }
}

总的来说,我尝试 post 使用 postman

这个原始的
{
   "Name" : "Roman Empire",
   "CountryCode" : "RE",
   "Population" : 22,
   "AreaSize": 22.0,
   "Capital" : "Rome",
   "Region" : "EU"
}

然后post男人给我一个结果:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
    "title": "Unsupported Media Type",
    "status": 415,
    "traceId": "|db653ff6-46b8be8826b56364."
}

Get 方法工作正常。哪里不对?请帮忙

特别适合您的情况,因为您使用 postman 来调用您的 API:

您需要将邮递员中的content-type设置为JSON(application/json)。转到 POST 请求中的正文,在那里你会找到原始选项。就在它旁边,会有一个下拉,select JSON.