0

I have a couple code which is responsible to post some data to web api. And I am trying to post a Dictionary. But the key property cannot be serialized when the request is posted.

There is my code :

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

There is fiddler session summary :

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"}

Where is the problem, any idea?

btungut
  • 23
  • 3
  • possible duplicate of [Serialize Class containing Dictionary member](http://stackoverflow.com/questions/495647/serialize-class-containing-dictionary-member) – VMAtm Feb 10 '15 at 10:40
  • You can't easily serialize class implementing the `IDictionary` – VMAtm Feb 10 '15 at 10:41

1 Answers1

0

It appears at first glance that you need to override ToString in your ItemDto implementation ... the default implementation from the system is to just print the name of the class, e.g., Foo.Contract.Items.ItemDto.

John Castleman
  • 1,552
  • 11
  • 12
  • Yes. But my expectation from PostAsJsonAsync method is serializing all properties properly. Actually it can be solved via your approaching. We can serialize itself in ToString method. Thanks :) – btungut Feb 10 '15 at 17:32
  • `HttpClientInstance` posting is just talking HTML, which is all text-based: it can't know how you want to "stringify" your `ItemDto`, so it relies on you to tell it by overriding `ToString`. [**Accepting Answers**](http://stackoverflow.com/help/accepted-answer). – John Castleman Feb 10 '15 at 20:03