Overview
The rest API, which is generated by rest (representational state transfer representational status transfer), is increasingly being discussed, and Microsoft has added the functionality of the Web API to ASP.
We are just looking at the use of the Web API and see if the current version solves the problem.
Project Establishment
After you have installed Visual Studio 2012, we click New Project--installed template->web->asp.net MVC 4 Web application to create a new project.
The project template selects the Web API.
In model we add the user class that was used in the previous article.
1Namespace Webapi.models
2 {
3public class Users
4 {
5 public int UserID { Span style= "COLOR: #0000ff" >get; set;}
6
7 public string UserName {get; set;}
8
9 public string useremail {get; set;}
10}
11}
Modifies the automatically generated valuecontroller to Userscontroller.
Get Data
Using the Get method of HTTP to request data, the request processing of the entire Web API is based on the MVC framework.
The code is as follows.
1Using System;
2Using System.Collections.Generic;
3Using System.Linq;
4Using System.Net;
5Using System.Net.Http;
6Using System.Web.Http;
7Using Webapi.models;
8
9Namespace Webapi.controllers
10 {
11PublicClass Userscontroller:apicontroller
12 {
13/// <summary>
14 ///User Data List
15 /// </summary>
16 PrivateReadOnly list<users> _userlist =New list<users>
17 {
18New Users {UserID =1, UserName ="Superman", UserEmail ="Superman@cnblogs.com"},
19New Users {UserID =2, UserName ="Spiderman", UserEmail ="Spiderman@cnblogs.com"},
20New Users {UserID =3, UserName ="Batman", UserEmail ="Batman@cnblogs.com"}
21};
22
23//GET api/users
24 Public ienumerable<users> Get ()
25 {
26return _userlist;
27}
28
29//GET API/USERS/5
30 Public Users Getuserbyid (int id)
31 {
32var user = _userlist.firstordefault (users + users. UserID = = ID);
33if (user = =Null
34 {
35ThrowNew Httpresponseexception (Httpstatuscode.notfound);
36}
37 return user;
38}
39
40 //get api/users/?username=xx
public ienumerable<users> getuserbyname (string userName)
42 {
43 Span style= "COLOR: #0000ff" >return _userlist.where (p = string. Equals (P.username, UserName, stringcomparison.ordinalignorecase));
44}
45}
46}
Constructs a user list, implements three methods, we make the request below.
When you use a different browser request, you find that the returned format is not the same.
Using the chrome request first, we found that the Content-type inside the HTTP header is an XML type.
We'll change the Firefox request and find out if Content-type is still an XML type.
We used the IE request again and found it to be so.
After opening the saved file, we find that the requested data is in JSON format.
The reason for this discrepancy is that the Content-type inconsistency in the request header sent by different browsers is caused.
We can use Fiddler to verify.
Content-type:text/json
Content-type:text/xml
Post data
Implement a user-added feature, accept the type of user entity, and our post data for the corresponding JSON data, see Dudu in the beta version of the problem encountered there is no solution.
1 //post api/users/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 the post request, we attach the code to the process and set the breakpoint at the Add method.
In Visual Studio 2012, the program for debug host becomes IIS Express.
We use ctrl+alt+p, which is attached to its process.
The following is a simulated post using Fiddler.
Note that the JSON content for Content-type in the request header is text/json,post:
1 {"UserID": 4, "UserName": "Parry", "UserEmail":P arry@cnblogs.com}
After clicking Execute and jumping to the breakpoint we set earlier, we look at the data submitted.
The problem with Dudu in beta is solved.
Conclusion
Asp. NET Framework has evolved all the way, it is true that functions are becoming more powerful and convenient. I hope we can abandon the argument of language, return to the purely technical discussion, all say that Microsoft's technology changes too fast, what is the essence of change? Is it good to be the same?
In the second part, we'll take a look at some of the security validation issues in the Web API.
Something wrong, hope to point out, discuss.
If you like, a recommendation is the best affirmation of the article. :)