1

嗨,我有一个将公开给 RestAPI 的函数。我需要传入一个 JSON 字符串作为定义的类。我将以下类用于 JSON 格式:

public class Registrant : Guest
{
    public int RegistrantID { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public int EventID { get; set; }
    public bool IsPrimary { get; set; }
}

我的控制器中有以下功能:

public HttpResponseMessage Create(string id, [FromBody]Registrant registrant)

但是,当我传入相同结构的 JSON 字符串时,它无法正确反序列化。

有什么问题吗?

PS:我在请求正文中的 JSON 字符串:

{ 
    "RegistrantID":"0",
    "Email": "abc@abc.com",
    "Phone": "123456789",
    "EventID": "8",
    "IsPrimary": "true",
    "CustomerId": "12345678",
    "FirstName": "abc",
    "LastName": "def"
}

更新:通过选择 Content-Type 作为 Application/Json 解决了这个问题另外,我去掉了 int 和 bool 参数上的引号,它工作正常。最后,项目中的 api 调用如下所示:

public HttpResponseMessage Create([FromUri]string id, [FromBody]Registrant registrant)
4

0 回答 0