ASP.NET MVC-WebAPI request

1. No parameter Get request

  For general get requests, we can use $.get() or $.ajax({type: "get"}) provided by jquery to achieve:

  The background Action method of the request is still GetUser() in the previous article:

  You can also use $.ajax({type:"get"}) to get the returned data correctly:

2. A Get request that passes a parameter

  Usually we need to pass parameters and only need to specify the data attribute of the ajax method: data:{"name":"Zhao Dabao"} 

  The background returns data correctly:

3. Get request passing two or more parameters

  According to the above method, we can easily write out multiple parameters: data:{"name":"Zhao Dabao","age":12}

   The background returns data correctly:

4. Post request without parameters

  We can use $.post() or $.ajax({type:"post"}) to initiate a post request:

  The background returns the data correctly:

5. Pass a Post request with a parameter:

  First of all, I need to remind everyone here that after we modify the background code, if the project is not regenerated, then an error will be reported when requesting: "No HTTP resource matching the request *** was found":

  Therefore, as long as we modify the background code, we must regenerate it:

  However, when we regenerated the project and sent the request again, we still saw a 404 error, checked the code again, and found no problem.

  In fact, ASP.NET Web API can correctly identify our UserController controller to handle Post /api/user, but can't find an acceptable method to handle the request.

That is to say, the request received by the Web API can find the User controller, but the Action named Def cannot be found in the controller.

So how do we solve it?

By searching the description on the official website of Web API on MSDN, we can find the following introduction:

That is, in the Action method, we need to use the [FromBody] attribute tag to indicate the attribute .

  After modification, send the request again, we can see that the Status Code is 200, and the request is sent successfully.

  It can be seen that in the post request, the parameters of the method must be modified with the [FromBody] attribute, and [FromBody] tells Web API to obtain the value of the parameter from the body of the post request.

But what surprised us is that the value of name in the data returned by the background is empty.

  Through debugging, we can see that the name value received in the background Action is null.

  Through the above test, I can also guess that the [FromBody] parameter that Web API requires to pass in the request must have a specific format in order to be correctly obtained. And this specific format is not our common key=value key-value pair form. The Web API's model binder expects to find a value without a key in [FromBody], that is, not key=value, but =value.

Now, we set the key in data to be empty, and then send the request again:

  It can be seen from the test that the data is received correctly in the background:

 6. Post request passing two parameters

  It stands to reason that if the request for one parameter is realized, it will be smooth to pass two or more parameters. For the background receiving method of two parameters, we may write it like this:

But it turns out that this is wrong.

So how do we define two or more parameters?

Looking at the introduction on the official website again, we learned that:

  That is to say, there can only be one parameter modified by [FromBody] . We need to encapsulate the multiple parameters passed.

Here, we can encapsulate Name and Age into a Student class:

  The front page sends the request again:

  The Status Code is 200, the request is successful, and the data returned by the background is correctly obtained:

  Here, we can see from the request header data in the above picture that the format of Form Data is key=value&key=value. This form data format is Name=%E8%B5%B5%E5%A4%A7%E5%AE%9D&Age =13 , we usually use more in json format. Here, we use JSON.stringify() to serialize the data.

Send the request again:

  It can be seen that the format of the data in From Data is a json string, and the Status Code is 200. The request is correct, but the result is wrong again, and the background has not received the data parameters:

So what is the problem?

  We looked at the content of the request header in the above figure again and noticed that although the data format we passed was a json string, the Content-type in the request header was 

application/x-www-form-urlencoded is not application/json for json format. The encoding format application/x-www-form-urlencoded means: form data is encoded as name/value pairs. 

  Here is a point of attention that I want to say. Usually when we use json data, it is easy to forget to specify the Content-type as "application/json", so it is easy to cause many "unexpected" errors.

So, we specify the Content-type in $.ajax():

  This time, the background correctly received and returned the data:

7. Post request passing multiple parameters

  With the above experience, we can easily write post requests that pass multiple parameters:

The background receives and returns data:

8. Pass multiple Post requests with different objects

  Sometimes we also encounter the need to pass multiple different object parameters. For this special case, Json.Net provides us with a general object container called JObject. We can pass the object name. Dynamically traverse the value of the attribute in the parameter, and then dynamically convert to the corresponding attribute type in the json type.

for example:

Background debugging, get the value of the attribute through dynamic dynamic conversion:

The background returns the data correctly:

9. Obtain different types of data

  Generally, the format type of the data returned by the background is json format, and we can also specify the output type in the request header to obtain different return type data:

Specify the output type as xml format:

Specify the output type as json format:

  Basically, here is the main content of this article. The key point is that the processing of parameters in Post requests needs to be paid attention to.

  In the process of testing above, we use the Controller controller class we created to receive and process the parameters. Some people may ask, whether our writing in this way complies with the specification, and how does the Web API handle it by default? Here, Visual Studio also provides us with a Web API controller class:

We can add a new item and select the Web API controller class:

  Here we can see that in the Controller class created by default, the Action method for the Post request has its own [FromBody] attribute. Now I don't need to say, you already know why it will have a [FromBody] attribute for the parameter by default!

Guess you like

Origin blog.csdn.net/qq_26695613/article/details/130699289