c# - How to make a post call to a Web Api Action? -
i've created web api action below
[httppost] public void load(string sitename, string providername, userdetails userdetails) { // implementation }
the route i've registered below (not sure if it's correct?):
config.routes.maphttproute( name: "loaduserdetails", routetemplate: "sites/{sitename}/user/load/{providername}/{userdetailslist}", defaults: new { controller = "user", action = "load", providername = urlparameter.optional });
the providername parameter should optional , i'm using xml serialization.
the action should response below url sample:
http://www.domain.com/sites/site1/user/load/provider1/[some user details in post] or http://www.domain.com/sites/site1/user/load/[some user details in post]
how make post call action can test service?
from: http request post
httpwebrequest request = (httpwebrequest)webrequest.create(@"http:\\domain.com\page.asp"); asciiencoding encoding = new asciiencoding(); string postdata = "username=user"; postdata += "&password=pass"; byte[] data = encoding.getbytes(postdata); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = data.length; using (stream stream = request.getrequeststream()) { stream.write(data,0,data.length); } httpwebresponse response = (httpwebresponse)request.getresponse(); string responsestring = new streamreader(response.getresponsestream()).readtoend();
Comments
Post a Comment