c# - ASP.Net web api post action param always coming null -
i getting null value post action param in asp.net web api.
this action.
[system.web.mvc.httppost] public httpresponsemessage add([frombody]products id) { var response = new httpresponsemessage(); try { if (id.productslist.length > 0) { response.statuscode = httpstatuscode.ok; response.ensuresuccessstatuscode(); response.content = new stringcontent(string.format("number of products {0}",id.productslist.length) ); logger.info(string.format("number of products {0}", id.productslist.length)); } response.statuscode = httpstatuscode.badrequest; } catch (exception ex) { response.statuscode = httpstatuscode.internalservererror; response.content = new stringcontent("error occured"); logger.error(ex); } return response; }
this how trying invoke api.
var filepath = @"c:\apps\eastworks\lott\boots.xml"; var xmldoc = new xmldocument(); xmldoc.load(filepath); var client = new httpclient(); mediatypeformatter jsonformatter = new xmlmediatypeformatter(); httpcontent content = new objectcontent<string>(xmldoc.outerxml, jsonformatter); if (content.headers.contains("content-type")) { content.headers.remove("content-type"); content.headers.add("content-type", "application/x-www-form-urlencoded"); } var result = client.postasync("http://localhost:3103/products/add", content) .result;
following model.
[xmlroot("products")] public class products { [xmlelement("product")] public product[] productslist { get; set; } } public class product { public product() { //default implementation } [xmlelement("code")] public string code { get; set; } [xmlelement("related-product")] public relatedproduct[] relatedproducts { get; set; } [xmlelement("description")] public string description { get; set; } // removed of properties. [xmlelement("variant")] public variant[] variants { get; set; } }
and xml.
<?xml version="1.0" encoding="utf-8"?> <products> <product> <code>mipacaloha</code> <related-product>paisley_blk</related-product> <related-product>mipacpolkadot</related-product> <description>classic mipac silhouette. 30cm (12 inches) wide 37cm (15 inches) high 15cm (6 inches) depth.</description> <brand>mi pac</brand> <style>backpack</style> <model-name>pocket prints</model-name> <weight>0.4</weight> <gender>womens</gender> <variant> <bag-details /> <exact-colour>aloha sky blue</exact-colour> <colour>blue</colour> <pic-url>005872</pic-url> <sku>136200</sku> <ean>5053466362002</ean> <stock>0</stock> <price>21.99</price> </variant> <variant> <bag-details /> <exact-colour>aloha purple</exact-colour> <colour>purple</colour> <pic-url>mipacaloha</pic-url> <sku>121521</sku> <ean>5053466215216</ean> <stock>6</stock> <price>18.99</price> <original-price>21.99</original-price> </variant> </product> </products>
i setting xmlserializer used in application_start.
var xml = globalconfiguration.configuration.formatters.xmlformatter; xml.usexmlserializer = true;
i had seen lot of posts similar mine. couldn't fix problem. please advise me.
thanks, naresh
are specifying web api use xmlserializer? default, uses datacontractserializer binding xml requests.
Comments
Post a Comment