Overview
More and more rest APIs are produced by rest (representational state transfer declarative state transfer). Microsoft has also added web API functions in ASP. NET.
We can see Dudu'sArticleHttpclient + ASP. net web API, another choice outside of WCF knows that the blog Park also began to use web API, and encountered this problem when using the web API beta version: infatuated accident: Asp. net webapi RC does not support the most common JSON parameter passing.
Let's take a look at the use of Web APIs and see if the current version has solved this problem.
For new features of ASP. NET web forms 4.5, see my previous article:
- New Features of ASP. NET web forms 4.5 (1): strong data control and bundling
- New Features of ASP. NET web forms 4.5 (2): HTML5 update and unobtrusive Validation
- New Features of ASP. NET web forms 4.5 (3): model binding
Project Creation
After Visual Studio 2012 is installed, choose Create Project> installed template> Web> Asp. Net MVC 4 Web application to create a project.
Select web API as the project template.
In the model, we also add the user class used in the previous article.
1 Namespace Webapi. Models
2 {
3 Public Class Users
4 {
5 Public Int Userid { Get ; Set ;}
6
7 Public String Username { Get ; Set ;}
8
9 Public String Useremail { Get ; Set ;}
10 }
11 }
Change the automatically generated valuecontroller to userscontroller.
Get Data
Http get requests are used to obtain data. The request processing of the entire web API is based on the MVC framework.
CodeAs follows.
1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. net;
5 Using System. net. HTTP;
6 Using System. Web. HTTP;
7 Using Webapi. models;
8
9 Namespace Webapi. Controllers
10 {
11 Public Class Userscontroller: apicontroller
12 {
13 /// <Summary>
14 /// User Data list
15 /// </Summary>
16 Private Readonly List <users> _ userlist = New List <users>
17 {
18 New Users {userid = 1 , Username = " Superman " , Useremail = " Superman@cnblogs.com " },
19 New Users {userid = 2 , Username = " Spiderman " , Useremail = " Spiderman@cnblogs.com " },
20 New Users {userid = 3 , Username = " Batman " , Useremail = " Batman@cnblogs.com " }
21 };
22
23 // Get API/users
24 Public Ienumerable <users> get ()
25 {
26 Return _ Userlist;
27 }
28
29 // Get API/users/5
30 Public Users getuserbyid ( Int ID)
31 {
32 VaR User = _ userlist. firstordefault (users => users. userid = ID );
33 If (User = Null )
34 {
35 Throw New Httpresponseexception (httpstatuscode. notfound );
36 }
37 Return User;
38 }
39
40 // Get API/users /? Username = xx
41 Public Ienumerable <users> getuserbyname ( String Username)
42 {
43 Return _ Userlist. Where (P => String . Equals (P. username, username, stringcomparison. ordinalignorecase ));
44 }
45 }
46 }
A user list is constructed and three methods are implemented. The following is the request.
The returned format varies with the browser request.
First, use Chrome requests. We found that the Content-Type in the HTTP header is of the XML type.
For another Firefox request, we found the Content-Type or XML type.
We use the IE request again and found that this is the case.
Open the saved file and we find that the requested data is in JSON format.
The reason for this difference is that the Content-Type in the request header sent by different browsers is inconsistent.
We can use fiddler for verification.
Content-Type: text/JSON
Content-Type: text/XML
Post Data
Implement the function of adding a user. The accepted type is the user entity, and the data we post is the corresponding JSON data to see if the problem encountered by Dudu in the beta version has been solved.
1 // Post API/users entity JSON
2 Public Users Add ([frombody] users Users)
3 {
4 If (Users = Null )
5 {
6 Throw New Httprequestexception ();
7 }
8 _ Userlist. Add (users );
9 Return Users;
10 }
We still use Fiddler to simulate post data.
Before a POST request, we first append the code to the process and set the breakpoint at the add method.
In Visual Studio 2012ProgramChanged to IIS Express.
We use CTRL + ALT + P to attach it to the process.
The following uses Fiddler to simulate post.
Note that the Content-Type in the request header is text/JSON, And the JSON content of post is:
1 {"Userid": 4, "username": "Parry", "useremail": Parry@cnblogs.com}
Click execute and jump to the breakpoint we set earlier. Let's take a look at the submitted data.
In this way, Dudu encountered problems in Beta.
Conclusion
The ASP. NET Framework has been developed along the way, and its functions are becoming more and more powerful and convenient. I hope that we can discard the language debate and return to purely technical discussions. We all say that Microsoft's technology is changing too fast. What is the essence of the change? Is it okay to stay the same?
In the second part, we will take a look at some security verification problems in Web APIs.
If something is wrong, I hope to point it out and discuss it.
If you like it, clicking a suggestion is the best affirmation of the article.
Download demo source code