the difference between Get and Post?

  1. The get parameter is passed through the url and the post is placed in the request body.
  2. The parameters passed by the get request in the url are limited in length, and the post is not.
  3. Get is less secure than post because the parameters are exposed directly to the url, so they cannot be used to pass sensitive information.
  4. The get request can only be url encoded, and the post supports multiple encoding methods.
  5. The get request browser will actively cache, and the post will not.
  6. The get request parameter will be completely retained in the browsing history, and the parameters in the post will not be retained.
  7. Get and post are qualitatively tcp links, no difference. However, due to HTTP regulations and browser/server limitations, they show some differences in the application process.
  8. Get generates a tcp packet; post generates two tcp packets. For the get mode request, the browser will send the http header and data together, the server responds with 200 (returning data); for post, the browser sends the header first, the server responds with 100 continue, the browser sends the data again, and the server responds. 200 ok (return data).

When doing data query, it is recommended to use get mode; when doing data addition, modification or deletion, it is recommended to use post mode.

Get request

No parameter request

1) Default mode

What is the default way? There is only one HttpGet request method in your entire Controller, the request path: api/[controller]. The code is as shown below: 

If there are two? The following figure and the front-end page return an abnormal result: the 

returned result indicates that there are multiple matches and an exception is reported. What is this? Obviously the two method names are different. In fact: When we use the VS2017 .net core to generate API projects, the ASP.NET Core API project uses attribute routing by default , not traditional routing. The traditional route will match the method Action according to the Url address.

Microsoft: When generating REST APIs, it supports attribute routing using the Http[Verb] attribute (which is what is described in this article), and rarely uses [Route(…)] on the method of operation. It is recommended to use the more specific Http[Verb] Attributes to clarify the operations supported by the API. Clients of the REST API need to know the path and Http predicate mapped to a particular logical operation. ( Personal Http[Verb] attribute routing is simpler than Route attribute routing )

2) Routing template settings

But the reality is, there can be only one httpGet method in my Controller, what should I do? The concept of routing a route template 
As shown in the following figure, you can see that the httpGet property provides a route template design, two optional. Template means a new routing template, Name can be a new HttpGetAttribute, Order can set the request processing order .

correspondingly, and the corresponding results are as follows:

Request

1) Default mode

If the id is the use of only one to acquire data, using can be achieved as shown, the request path in this way is simple, access paths: api / values / id, inside the braces {} names must be consistent with the parameter name 

as described above The above can not meet the multiple parameter request method of get, if there are multiple parameters and use get request, it is necessary to return to the old routine, as shown below

2) Routing template settings

Similarly, the new routing template carries the simple parameter nameid that is convenient for the request. The {} name in the braces must be consistent with the parameter name.

The above figure can not meet the multiple parameter request mode of the new routing template get. If there are multiple parameters and use the get request, it is necessary to return to the old routine, as shown in the following figure.

Post request

No parameter request

1) Default mode

As shown in the following figure, since the request path is /api/values, the entire controller will look for the Post request 
request path: /api/[controller] 

but if there is another Post method at this time, there is no new template renamed, either If there are no parameters, they will report an error. Because the api/values ​​request path will only look for POST requests. So to make the following routing template settings

2) Routing template settings

The same as the routing template setting of the httpGet request mode, there are three parameters of template, Name, and Order, and the latter two are optional. 
Request path: /api/values/PostMethod

Request

1) Default mode

I tried a lot of methods and finally found the following. The request path: /api/[controller] 

Both Ajax and PostMan requests require contentType: “application/x-www-form-urlencoded”, the focus is on not using [ApiController] and [FromBody] Make a modification (for the time being, don’t comment, I think it’s better to use Get). The 
Ajax request code is as follows:

  $.ajax({
                type: "POST",
                accepts: "application/x-www-form-urlencoded",
                url: "/api/post",
                contentType: "application/x-www-form-urlencoded",
                data: {
                    'value': 'jack'
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("Something went wrong!");
                },
                success: function (result) {
                }
            });

2) Routing template settings

The same as the routing template setting of the httpGet request mode, there are three parameters of template, Name, and Order, and the latter two are optional. 
Request path: /api/[controller]/PostName

3) Combination parameter request

If there are complex type parameters and simple type parameters exist at the same time, what should I do?

At this point, the request controller should have the display attribute [ApiController]. The request method is as follows: 
Request path: /api/todo?name=xiaoming&id=20

4) Dynamic type

As shown in the following figure, it is a type parameter that supports dynamic, contentType: “application/json” 
request path: /api/todo