C# MVC4WebAPI如何为请求准备JSON正文
嗨,我有一个函数,它将向RestAPI公开。我需要传入一个JSON字符串作为已定义的类。对于JSON格式,我使用以下类:C# MVC4WebAPI如何为请求准备JSON正文,c#,json,rest,asp.net-mvc-4,C#,Json,Rest,Asp.net Mvc 4,嗨,我有一个函数,它将向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 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"
}
更新: 通过将内容类型选择为Application/Json,可以解决这个问题 此外,我还引用了int和bool参数,效果很好。 最后,api调用在项目中如下所示:
public HttpResponseMessage Create([FromUri]string id, [FromBody]Registrant registrant)
尝试删除int/bool类型的引号。我还假设您的映射是正确的,并且查询中包含一个id。它由choose Content Type:application/JSON解析。你的评论也是对的。我取出了引文,它就起作用了。