json - ASP.NET Web API Posted value not automatically map to object -
i'm trying send request json content web api, bit these value isn't automatically map object.
this api action result in null.
    [httppost]     public ienumerable<string> getcustomerbyname([frombody] request_getcustomerbyname request)     {         // action     }   if change parameter below can receive data fine. wonder why json string not automatically map object.
    [httppost]     public ienumerable<string> getcustomerbyname([frombody] dynamic request)     {         // action     }   this send request .
    public actionresult index()     {         using (var client = new httpclient())         {             client.baseaddress = new uri("http://localhost:40175/");             client.defaultrequestheaders.accept.clear();             client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));              request_getcustomerbyname r = new request_getcustomerbyname()             {                 customer = new customer() { customerid = 1, customername = "name" },                 requestbase = new requestbase() { somefield="123"}             };              var json = new javascriptserializer().serialize(r);              httpresponsemessage response = client.postasjsonasync("api/values/getcustomerbyname", json).result;             if (response.issuccessstatuscode)             {                 var resval = response.content.readasstringasync().result;                 response.write(resval);             }         }         return view();     }   thanks, i've been stuck @ point hour...
you should inspect json string, online json c# object mapper might help: http://json2csharp.com/.
Comments
Post a Comment