Explore how to manipulate ASP. NET Web API (i)

Source: Internet
Author: User

Parry
Source:http://www.cnblogs.com/parry/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

This is a beginner's introduction of a very good article, highly recommended! Contact me add: jkxx123321 or follow the public number: love to enjoy the dark horse

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 look at Dudu's article HttpClient + ASP. The other option outside of WCF knows that the blog park is also starting to use the Web API and is experiencing this problem when using the beta version of the Web API: Infatuation: ASP. NET WebAPI RC does not support the most commonly used JSON parameters.

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 {
3PublicClass Users
4 {
5Publicint UserID {Get set;}
6
7 Public string UserName { get; set;}
8
9 Public string UserEmail { get; set;}
10}
One }

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>
16PrivateReadOnly list<users> _userlist =New list<users>
17 {
18New Users {UserID =1, UserName ="Superman", UserEmail ="[Email protected]"},
19New Users {UserID =2, UserName ="Spiderman", UserEmail ="[Email protected]"},
20New Users {UserID =3, UserName ="Batman", UserEmail ="[Email protected]"}
21};
22
23//GET api/users
24Public ienumerable<users> Get ()
25 {
26return _userlist;
27}
28
29//GET API/USERS/5
30Public Users Getuserbyid (int id)
31 {
32var user = _userlist.firstordefault (users + users. UserID = = ID);
33if (user = =Null
34 {
35ThrowNew Httpresponseexception (Httpstatuscode.notfound);
36}
37return 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&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
45&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
46&NBSP;}

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
2public users add ([frombody]users users)
 3 {
 4      if  (Users == null)
 5     {
 6          throw  new httprequestexception ();
&NBSP;7&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
  8     _userlist.add (users);
 9     return  users;
10&NBSP;}

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":[email protected]}

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. :)

Explore how to manipulate ASP. NET Web API (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.