Web 中的模型绑定器 Api 不绑定我的 Fiddler Json post 请求

Model Binder in Web Api does not bind my Fiddler Json post request

我正在使用 Web Api 和 .Net Core。我有一个看起来像这样的 cass :

 public class Event
{
    public int Id { get; set; }
    public int? ParentId { get; set; }
}

然后我在这样的控制器中执行操作:

    [HttpPost]
    public void Post([ModelBinder] Event evt)
    {
    }

然后我用 Fiddler 编写了一个 Post 请求,如下所示:

User-Agent: Fiddler
Host: localhost:8186
Content-Length: 35
Content-Type: application/json; charset=utf-8

{
"Id":"1",
"ParentId":"0",
}

现在当断点命中我的动作时,模型绑定器不绑定我的对象!可能出了什么问题?

改为指定 [FromBody] 属性:

[HttpPost]
public IActionResult Post([FromBody] Event e)
{
    return Ok(e);
}

如果您发送 json 请求,这将正确绑定到您的模型。