使用 Fiddler 测试 WebApi POST

Testing WebApi with Fiddler POST

GET 工作正常。在控制器中我有这个方法:

    [HttpPost]
    public IHttpActionResult Post(Product product)
    {
        var isSaved = productService.SaveProduct(product);
        if (isSaved == true) return Ok();
        else return BadRequest(); 
    }

问题是,产品为空。我得到这个方法(通过断点)但我不确定为什么产品为空。以下是 Fiddlers Composer 中的内容:

User-Agent: Fiddler
host: localhost:53882
Content-Type: application/json; charset=utf-8
Content-Length: 54

这是 Fiddler 请求正文:

{
id:5,
name: Dress,
price: 39.90,
quantity:71
}

没有模型。这一切都在数据库(edmx)中。它适用于获取产品列表,或按产品 ID 获取产品。

您请求正文中的 name 缺少双引号。确保大小写与您的模型匹配,因为它区分大小写。

{
  id:5,
  Name: "Dress",
  Price: 39.90,
  Quantity:71
}

我认为问题可能出在 contentType 上,您应该在 header Content-Type: application/json

是的,您发布的 json 也是无效的:姓名:"Dress",