6
我从小提琴手发送POST:Asp.net芯柱参数始终是空
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方法总是很空。
// 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; }
}
我尝试添加NullValueHandling到ConfigureServices为post,但没有使用建议。
services.AddMvc()
.AddJsonOptions(jsonOptions =>
{
jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
尝试发送所有属性。包含'ProductID'和'CategoryID'。 –
这就像你映射字段不匹配,名称应该与产品类相同,也尝试删除'[FromBody]' – Bharat