Web API: JSON binds, XML doesn't on POST -
i have web api service (to surprise) needs able accept xml json. first, here models:
[datacontract] public class serializedcustomerevent { [datamember] public string typeid { get; set; } [datamember] public contextpair[] context { get; set; } } public class contextpair { public string key { get; set; } public string value { get; set; } }
here api controller method:
public void post(serializedcustomerevent value) { _queuebroker.queue(value); }
now here's part i'm overlooking something. json post fiddler works fine:
content-type: application/json; charset=utf-8 { "typeid":"abc", "context": [ {"key":"field1","value":"123"}, {"key":"field2","value":"jeff"} ] }
the xml version, however, doesn't work. context property null.
content-type: application/xml; charset=utf-8 <?xml version="1.0"?> <serializedcustomerevent xmlns="http://schemas.datacontract.org/2004/07/mynamespace"> <typeid>xmlwow</typeid> <context> <contextpair> <key>field1</key> <value>123</value> </contextpair> <contextpair> <key>field2</key> <value>jeff</value> </contextpair> </context> </serializedcustomerevent>
what missing here?
see this post: it's because data contract expects members in alphabetical order. if swap typeid , context elements around in source they'll both populated in object.
Comments
Post a Comment