2012-07-17 123 views
0

我的工作传递给MVC4 RC微软的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, “数据” 字段是空的,像这样:

Empty data

(没关系用于表和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版本中找到了一些例子。

+0

我不认为[FormUrlEncodedMediaTypeFormatter](https://aspnetwebstack.codeplex.com/SourceControl/changeset/view/576083242381#src%2fSystem.Net.Http.Formatting%2fFormatting%2fFormUrlEncodedMediaTypeFormatter.cs)可以处理这个。 – 2012-07-20 20:06:55

+0

@AlexanderZeitler作为回答作出回应,我会将其标记为正确。 – 2012-08-03 20:12:28

回答

1

我不认为FormUrlEncodedMediaTypeFormatter处理这个。