反序列化键:值对字典

时间:2012-07-17 22:00:07

标签: serialization asp.net-mvc-4 asp.net-web-api

我正在将传递给MVC4 RC中的Microsoft Web API的数据反序列化为以下类的对象:

public class EditorCreateEditSubmission
{
    public string action { get; set; }
    public string table { get; set; }
    public string id { get; set; }
    public Dictionary<string, string> data { get; set; }
}

每当Web API方法获取应映射到EditorCreateEditSubmission的数据时,“data”字段为空,如下所示:

Empty data

(Table和ID可以为空)

我的控制器方法:

public EditorServerResponse Post(EditorCreateEditSubmission ajaxSubmission)
{
//...Handle data
}

原始标题:

POST http://localhost:64619/API/Species HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:64619/Manage/Species
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost:64619
Content-Length: 134
Connection: Keep-Alive
Pragma: no-cache

action=create&table=&id=&data%5Bamu%5D=1&data%5BchemicalFormula%5D=H&data%5BcommonName%5D=Hydrogen&data%5Bstatus%5D=N&data%5Bnotes%5D=

更具可读性的观点:

action  create
table   
id  
data[amu]   1
data[chemicalFormula]   H
data[commonName]    Hydrogen
data[status]    N
data[notes] 

我是否需要手动创建一个具有get / set值的类,每个可能的传入值集合?似乎将这些数据反序列化为字典应该是直截了当的,但我在微软MVC4的新RC版本中找到示例时遇到了一些困难。

1 个答案:

答案 0 :(得分:1)

我不认为FormUrlEncodedMediaTypeFormatter会处理这个问题。