c# - RestSharp / ASP.NET WebAPI - Using POST with url parameters -
i've following asp.net webapi binding:
config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{action}/{id}", defaults: new {id = routeparameter.optional} );
and controller looks this:
public class referencedatacontroller : basecontroller { [requireusertoken(approveddevicetoken = true, validusertoken = true)] [httppost] public ienumerable<synchronizeitem<ireferencedataitem>> sync([frombody]ienumerable<synchronizeitem<ireferencedataitem>> clientsyncitems, [fromuri]int referencedatatype) { // code }
on client site use following code send request:
var client = new restclient (baseurl); var request = new restrequest (resource, method); request.xmlserializer = new jsonserializer (); request.requestformat = dataformat.json; request.addheader ("x-abc-devicetoken", devicetoken); if (!string.isnullorwhitespace (usertoken)) request.addheader ("x-abc-usertoken", usertoken); if (payload != null) request.addbody (payload); if (parameters != null) { foreach (var parameter in parameters) { request.addurlsegment(parameter.key, parameter.value); } } var response = client.execute<t> (request);
my expectation is, sending post request http://myhost/api/referencedata/sync?referencedatatype=countries
body contains ienumerable. if remove urlsegment parameters on client site , second argument on webservice site, works.
how can combine body payload , additional url parameters?
you can define action method follow,
[requireusertoken(approveddevicetoken = true, validusertoken = true)] [httppost] public ienumerable<synchronizeitem<ireferencedataitem>> sync(ienumerable<synchronizeitem<ireferencedataitem>> clientsyncitems, int referencedatatype) { // code }
no bodyattribute or fromuriattribute. in way, web api try use mediatypeformatter deserialize body clientsyncitems collection , additional value type query string (referencedatatype query string). route defined take "sync" id (which ignored not parameter in action).
you must specify content-type header web api can choose right formatter (json or xml example).
Comments
Post a Comment