MVC 4 API Calls are many, the most common and simplest is to use Ajax in the front-end interface call, if it is called in the background code, is to be more complex, the following is the way to call the API in the post-encapsulation method:
<summary>/// API call public method///</summary>// <param name= "url" > API URL </param> // <param name= "param" > Parameters </param>// <returns></returns> public static Httpresponsemessage postapi (string url, dictionary<string, string> param) { var dict = new dictionary< String, string> (); Dict. Add ("", Newtonsoft.Json.JsonConvert.SerializeObject (param)); var content = new Formurlencodedcontent (dict); var httpClient = new HttpClient (); Return Httpclient.postasync (URL, content). Result; }
String url = "Apiurl"; API URL var dict = new dictionary<string, string> (); Dict. ADD ("name", "Wisdomhu"); Dict. ADD ("Age", "7"); Httpresponsemessage response = Postapi (URL, dict); Console.WriteLine ("{0}", Response. StatusCode);
Calling the MVC 4 API in code