Asp.net Core Post parameter is always null
I'm sending POST from fiddler:
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"}
But Post method always gets null.
// POST api/Product
[HttpPost]
public IActionResult PostProduct([FromBody]Product product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_repo.Add(product);
return CreatedAtRoute("GetToode",product);
}
When I use [FormBody] product is always null, when not using it product is valued but with all fields being null. Product class is simple.
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; }
}
i tried adding NullValueHandling to ConfigureServices as proposed in post but no use.
services.AddMvc()
.AddJsonOptions(jsonOptions =>
{
jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
I just had to correct the double quotes in your POST request and it worked. Try this:
{"Ean":"1122u88991","Name":"Post test","Description":"Post test desc"}
See screenshot below.
链接地址: http://www.djcxy.com/p/93400.html