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