c# - How to serialise Web API 2 HttpRequestMessage into my custom object -
i have following method in webapi 2 project.
public class testcontroller : apicontroller { [httppost] public void test(httprequestmessage request) { var content = request.content; string jsoncontent = content.readasstringasync().result; } }
my custom object looks this;
public class test { public int id { get; set; } public string name { get; set; } }
if post sample data like
<test> <id>12345</id> <name>my name</name> </test>
the resulting value in jsoncontent correct. question how best should serialise httprequestmessage (content) object test can perform additional validation / tasks etc.
should pass httprequestmessage method or possible pass like
public void test(test otest)
you can use parameter in action method this.
[httppost] public void test(test otest)
asp.net web api deserialize request message body (json or xml) test
. based on content type header in request, web api can handle both json , xml content out-of-box. in case of xml, web api uses dcs, default. xml have shown in post not deserialized as-is. try returning test
object , see how gets serialized web api , use same xml in post request binding work correctly.
btw, if use test
parameter in action method, web api consume request body stream. so, not able read inside action method doing.
Comments
Post a Comment