The Webapi in MVC

Source: Internet
Author: User

The use of Webapi in MVC and how to invoke it in other sites (MVC)

1. WEBAPI routing rules are registered in the App_start\webapiconfig.cs file

2. Webapi Controller inherits the parent class Apicontroller

3, call Webapi way: Get request HTTP://LOCALHOST/API/HOME/1 is the default request from the home controller get method will be 1 as a parameter of the Get () method POST request Http://localhost/api/home /1 Default request the Post method under the home controller will use 1 as the parameter of the post () method

4. Set WEBAPI default return format to JSON format public static class Webapiconfig {public static void Register (httpconfiguration Config) {//Xmlformatter in Webapi is removed, by default, Jsonformatter is used as its transport format config. Formatters.remove (config.   Formatters.xmlformatter); }

5. Request using HttpWebRequest request in another Web site Webapi example://Simulate browser request Http://localhost:55749/api/values/GetPig pass in the specified ID parameter value string re Questurl = "http://localhost:15405/infos.ashx?id=" + txtID. Text;

1.0 instance of the instantiated Web request object WebRequest request = WebRequest.Create (Requesturl); 2.0 set its request method to get request requests.   Method = "Get"; 3.0 Gets the response message object returned by the server response WebResponse response = Request.   GetResponse (); System.IO.Stream str = response. GetResponseStream ();

Convert the flow to string responsebody = ""; using (System.IO.StreamReader SRD = new System.IO.StreamReader (str)) {//Converts the data in the Echo style into a string RESPONSEBODY=SRD.   ReadToEnd (); }

Response.Write (Responsebody);

The Webapi in MVC

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.