ASP. net mvc web api mandatory knowledge points summary, mvc required
1. Understanding WEB APIs: Provides RESTful-based WEB Services and maps them to the corresponding ACTION method (CRUD) on the server through HTTP request methods (GET, PUT, POST, and DELETE ).
RESTful architecture:
(1) Each URI represents a resource;
(2) A performance Layer for transferring such resources between the client and the server;
(3) The client uses four HTTP verbs to perform operations on server resources and realize "transition of performance Layer status ".
The four main methods (GET, PUT, POST, and DELETE) of HTTP are mapped to CURD as follows:
GET is used to obtain URI resources for display. GET operations should not have any impact on the server;
PUT is used to update a resource on the URI. If the server permits, PUT can also be used to create a resource;
POST is used to create a resource. The server creates a new object on the specified URI and returns the address of the new resource as part of the response message;
DELETE is used to DELETE a specified URI resource.
Ii. web api features:
1. The CONTROL class inherits from the abstract class ApiController;
2. When registering a route, you generally do not need to specify the ACTION node. The ACTION method name generally includes the HTTP request method name. The Routing System Automatically finds and executes the corresponding ACTION method through the HTTP request method;
3. the return value of the ACTION method is generally JSON, XML, or common value object.
3. Implement the methods for sending GET, PUT, POST, and delete http requests
1. Use the JQUERY. AJAX method to specify the TYPE to implement the GET, PUT, POST, and delete http request methods;
2. You can directly access the URL or set the METHOD of the form to GET to implement the get http request METHOD;
3. Set the form METHOD to POST to implement the post http request METHOD;
4. in addition to the first method, PUT and DELETE can only be implemented by rewriting the HTTP Request Method (custom HttpMessageHandler) on the server ), then, specify "X-HTTP-Method-Override" in the request header of the client.
The value is PUT or DELETE. For the specific implementation method, see: What if ASP. NET Web API cannot be called to send PUT/DELETE requests?
5. Specify the ACTION node when registering a web api routing rule;
IV. Implementation of web api requests and server processing:
1. get all method:
Client:
$("#Button1").click(function () { $.getJSON("@Url.Content("~/api/values")", function (data) { var rs = ""; $.each(data, function () { rs += this + ","; }) alert(data); showResult(rs); }) });
Server:
// GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; }
2. get one method:
Client:
$("#Button2").click(function () { $.getJSON("@Url.Content("~/api/values/5")", function (data) { alert(data); showResult(data); }) });
Server:
// GET api/values/5 public string Get(int id) { return "value is " + id.ToString(); }
3. post create method: (note that the first methods in the following client correspond to the first methods on the server)
Client:
// First: $ ("# Button1"). click (function () {$. post ("@ Url. Content ("~ /Api/values ")", {name: 'zwj', age: 29}, function (data) {alert (data); showResult (data );})}); // type 2: $ ("# Button3 "). click (function () {$. ajax ("@ Url. content ("~ /Api/values/1 ")", {type: 'post', data: JSON. stringify ({name: 'zwj', age: 29}), contentType: 'application/json', // dataType: 'json', success: function (result, status, xhr) {alert (result); showResult (result );}})});
Server:
// Method 1: public string Post () {string s = ""; HttpContextBase context = (HttpContextBase) Request. properties ["MS_HttpContext"]; // obtain the traditional context HttpRequestBase request = context. request; // define the traditional request object for (int I = 0; I <request. form. keys. count; I ++) {s + = string. format ("{0 }={ 1} <br/>", request. form. keys [I], request. form [I]);} return "Post values:" + s;} // Method 2: public string Post ([FromBody] Person p) {return string. format ("Put values: name: {0}, age: {1}" + p. name, p. age );}
4. put update method:
The client method is the same as the POST method, but the TYPE is specified as: PUT;
The server side and the POST method are the same;
5. DELETE method:
The client method is the same as the GET method, but the TYPE is specified as: DELETE;
The server and GET methods are the same;
See also this article: ASP. net mvc Learning Series (II)-WebAPI requests