asp.net mvc - web api call from code behind -
i new mvc web api
i have crated web api post method takes object type "bag" , return htmlstring code shown bellow
public htmlstring postbag(bag bagofitem) { return utility.postbagdiscounteditem(bagofitem); }
now web site wanted call api method postbag controller postbag()
and not know how , appreciate if 1 can show me how this
what have got in web application bellow.
public class homecontroller : controller { private bag _bag = new bag(); private string uri = "http://localhost:54460/"; public actionresult postbag() { // 1 show me how post _bag api method postbag() return view(); } public class bag { private static list<product> _bag { get; set; } public list<product> getbag () { if (_bag == null) _bag = new list<product>(); return _bag; } }
try this:
using (var client = new httpclient()) { client.baseaddress = new uri("http://localhost:54460/"); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpresponsemessage response = await client.getasync(_bag); if (response.issuccessstatuscode) { console.writeline("success"); } else { console.writeline("error feed"); } }
Comments
Post a Comment