<分区>
我正在从 fiddler 发送 POST:
POST http://localhost:55924/api/Product HTTP/1.1
User-Agent: Fiddler
Host: localhost:55924
Content-Type: application/json; charset=utf-8
Content-Length: 84
{"Ean″:”1122u88991″,”Name″:”Post test″,"Description":"Post test desc"}
但是 Post 方法总是返回 null。
// POST api/Product
[HttpPost]
public IActionResult PostProduct([FromBody]Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_repo.Add(product);
return CreatedAtRoute("GetToode",product);
}
当我使用 [FormBody] 时,产品始终为空,当不使用它时,产品有值但所有字段都为空。产品类别简单。
public class Product
{
public int ProductID { get; set; }
public string EAN { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int? CategoryID { get; set; }
}
我尝试按照 post 中的建议将 NullValueHandling 添加到 ConfigureServices但没有用。
services.AddMvc()
.AddJsonOptions(jsonOptions =>
{
jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});