c# - Send variables to Web API method -
i have set basic mvc web api project following method in valuescontroller class -
// api/values/5 public string get(int id) { return "success!"; } and calling in client -
client.baseaddress = new uri("http://localhost:12345/"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpresponsemessage response = await client.getasync("api/values/5"); if (response.issuccessstatuscode) { result = await response.content.readasstringasync(); } i relatively new t mvc trying modify method accept string variable used in database call. instance -
// api/values/number/house/street public string get(string no, string house, string street) { // use variables return "success!"; } then client -
httpresponsemessage response = await client.getasync("api/values/" + no + house + street); if (response.issuccessstatuscode) { result = await response.content.readasstringasync(); } i trying use api webservice unsure how modify work in such way.
when tried above code returned bad request.
you haven't setup routing webapi service. check out resource on how set up: http://www.asp.net/web-api/overview/web-api-routing-and-actions
edit:
or, wait! if may call service wrong. if @ api method definition comment tells url should follows:
get api/values/number/house/street while looks call as:
get api/values/nohousestreet so there no slash between values. guessing if change url call following things should work properly:
httpresponsemessage response = await client.getasync("api/values/" + no + "/" + house + "/" + street);
Comments
Post a Comment