MVC 4 Web API如何为Request准备JSON主体

时间:2013-06-07 15:22:25

标签: 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 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 params上的引号,它运行正常。 最后api调用在项目中看起来像这样:

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

0 个答案:

没有答案