Understanding Web API: Provide a restful architecture-based Web service that maps to the appropriate action method (CRUD) on the server side via the HTTP request method (GET, PUT, POST, DELETE).
RESTful architecture:
(1) Each URI represents a resource;
(2) between the client and the server, the transmission of such resources of some kind of performance layer;
(3) The client through four HTTP verbs, the server-side resources to operate, to achieve "performance layer State transformation."
The four main methods of HTTP (GET, PUT, POST, DELETE) are mapped to curd operations in the following ways:
Get is used to obtain the presentation of the URI resource, and the get operation should not have any effect on the server;
Put is used to update a resource on a URI, and a put can be used to create a new resource if the server allows it;
POST is used to create a new resource, and the server creates an object on the specified URI, returning the address of the new resource as part of the response message;
Delete is used to delete the specified URI resource.
Second, the WEB API features:
The 1.CONTROL class inherits from Apicontroller abstract class;
2. When you register a route, you typically do not need to specify an action node, and the action method name typically contains the name of the HTTP request method name, and the routing system automatically finds and executes the corresponding action method through the HTTP request method;
3.ACTION method Return values are generally: JSON, XML, or generic value objects
Third, implement send Get, PUT, POST, DELETE http request method
1. Implement get, PUT, POST, DELETE http request method by the Jquery.ajax method specifying type types;
2. The Get HTTP request method can be implemented by directly accessing the URL or by setting the method of the form to get;
3. If the form method is set to post, the Post HTTP request method can be implemented;
4.PUT, delete In addition to the first method, can only be done by overriding the HTTP request method on the server (custom Httpmessagehandler), and then in the client request message header specified "X-http-method-override
"Value is put or delete to implement; see how to do this if calling the ASP. NET Web API cannot send a PUT/DELETE request?
5. Specify the action node when registering the Web API routing rules;
Iv. WEB API request and server-side processing implementation method:
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-side:
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-side:
Get API/VALUES/5 public string get (int id) { return ' value is ' + ID. ToString (); }
3.POST Create method: (Note the following methods in the client to correspond to the server side of the first method)
Client:
The first type: $ ("#Button1"). Click (function () { $.post ("@Url. Content (" ~/api/values ")", {name: ' Zwj ', age:29},function ( Data) { alert (data); Showresult (data);}) }); /second type: $ ("#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-side:
First method: Public string Post () { string s = ""; HttpContextBase context = (httpcontextbase) request.properties["Ms_httpcontext"];//get traditional context Httprequestbase Request = context. request;//defines 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; } The second method: 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, except that the type is specified as: PUT;
The server side is the same as the Post method;
5.DELETE Method:
The client method is the same as the Get method, except that the type is specified as: DELETE;
The server side is the same as the Get method;
See also this article: ASP. NET MVC Learning Series (ii)-WEBAPI request
ASP. NET MVC WEB API must know the Knowledge point summary