2012-07-17 147 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處理這個。