Calling the MVC 4 API in code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.