2015-02-10 65 views
0

我有一些代码负责将一些数据发布到web api。我正在尝试发布词典。但是当请求被发布时,key属性不能被序列化。PostAsJsonAsync无法序列化字典中的对象

有我的代码:

response = await base.HttpClientInstance.PostAsJsonAsync<Dictionary<ItemDto, string>>(url, dummyItems); 

有小提琴手会议纪要:

Content-Type: application/json; charset=utf-8 
Host: debug.foo.foo.com 
Content-Length: 78 
Expect: 100-continue 
Connection: Keep-Alive 

{"Foo.Contract.Items.ItemDto":"87a0a711-6ecb-4ef5-9aed-04f7573344e1"} 

问题出在哪里,什么想法?

+0

[Serialize Class contains Dictionary member]可能的重复(http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member) – VMAtm 2015-02-10 10:40:49

+0

你不能轻易地序列化实现IDictionary的类 – VMAtm 2015-02-10 10:41:09

回答

0

乍一看,您需要在您的ItemDto实现中覆盖ToString ...系统的默认实现是仅打印类的名称,例如Foo.Contract.Items.ItemDto

+0

是。但是我对PostAsJsonAsync方法的期望是正确地序列化所有属性。其实它可以通过你的接近来解决。我们可以使用ToString方法序列化自己。谢谢:) – btungut 2015-02-10 17:32:42

+0

'HttpClientInstance'发布只是在说HTML,它全是基于文本的:它不知道你想如何“串化”你的'ItemDto',所以它依靠你通过重写'ToString'来告诉它。 [**接受答案**](http://stackoverflow.com/help/accepted-answer)。 – 2015-02-10 20:03:30