ASP (i): Using the first approach, get and post data

Source: Internet
Author: User

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.

The new features for ASP. NET Web Forms 4.5 can be found in my previous article:

    • new features of ASP. NET Web Forms 4.5 (i): Strongly typed data controls and bundling
    • new features of ASP. NET Web Forms 4.5 (ii): Updates and unobtrusive for HTML5 Validation
    • new features of ASP. NET Web Forms 4.5 (iii): Model Binding
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.

1namespaceWebapi.models
2{
3 PublicclassUsers
4{
5 Public intUserID {Get;Set; }
6
7 PublicstringUserName {Get;Set; }
8
9 PublicstringUserEmail {Get;Set; }
Ten}
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.

1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Net;
5usingSystem.Net.Http;
6usingSystem.Web.Http;
7 usingWebapi.models;
8
9namespaceWebapi.controllers
Ten{
One PublicclassUserscontroller:apicontroller
A{
- ///   <summary>
-         /// User Data List
the         ///   </summary>
-PrivateReadOnlyList<users> _userlist =NewList<users>
-{
-NewUsers {UserID =1, UserName =" Superman ", UserEmail =" [email protected] "},
+NewUsers {UserID =2, UserName =" Spiderman ", UserEmail =" [email protected] "},
-NewUsers {UserID =3, UserName =" Batman ", UserEmail =" [email protected] "}
+};
A
at// GET api/users
- PublicIenumerable<users> Get ()
-{
- return_userlist;
-}
-
in// GET API/USERS/5
- PublicUsers Getuserbyid (intId
to{
+varuser = _userlist.firstordefault (users = users. UserID = = ID);
-if(User = =NULL)
the{
*ThrowNewHttpresponseexception (Httpstatuscode.notfound);
$}
Panax NotoginsengreturnUser
-}
the
+// GET api/users/?username=xx
A PublicIenumerable<users> Getuserbyname (stringUserName)
the{
+ return_userlist.where (p =string. Equals (P.username, UserName, stringcomparison.ordinalignorecase));
-}
$}
$}

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 PublicUsers Add ([frombody]users users)
3{
4if(Users = =NULL)
5{
6ThrowNewHttprequestexception ();
7}
8_userlist.add (users);
9returnUsers
Ten}

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

Demo Source Download

Reference page:hTTP://QINgQINgQuege.CNbLogs.Com/P/5933752.hTmL

ASP (i): Using the first approach, get and post data

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.