2016-09-28 42 views
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; 
    }); 
+1

尝试发送所有属性。包含'ProductID'和'CategoryID'。 –

+0

这就像你映射字段不匹配,名称应该与产品类相同,也尝试删除'[FromBody]' – Bharat

回答

3

我只是要纠正你的POST请求中的双引号,它的工作。试试这个:

{"Ean":"1122u88991","Name":"Post test","Description":"Post test desc"} 

请看下面的截图。

Screenshot

+0

老实说,我现在觉得这么愚蠢,我没有注意到它自己。 – martinv