Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ASP.NET Web API

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use ASP.NET Web API, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Overview

There are more and more discussions about REST API generated by REST (Representational State Transfer declarative State transfer). Microsoft has also added the function of Web API in ASP.NET.

Let's just take a look at the use of Web API and see if the current version has solved this problem.

Project establishment

After installing Visual Studio 2012, we click New Project-> installed templates-> Web- > ASP.NET MVC 4 Web Application to create a new project.

Select Web API as the project template.

In Model, we still 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;}

six

7 public string UserName {get; set;}

eight

9 public string UserEmail {get; set;}

10}

11}

Modify the automatically generated ValueController to UsersController.

GET data

The get method of HTTP is used to request to get data, and the request processing of the whole Web API is based on the MVC framework.

The code is as 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

eight

9 namespace WebAPI.Controllers

10 {

11 public class UsersController: ApiController

12 {

13 /

14 / User Data List

15 /

16 private readonly List _ userList = new List

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}

twenty-two

23 / / GET api/Users

24 public IEnumerable Get ()

25 {

26 return _ userList

27}

twenty-eight

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}

thirty-nine

40 / / GET api/Users/?username=xx

41 public IEnumerable 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. Let's make a request.

You will find that the format of the return is different during the request using different browsers.

Using the Chrome request first, we find that the Content-Type in HTTP Header is of type xml.

We change the FireFox request and find that Content-Type is still of type xml.

We use the IE request again and find that this is the case.

Opening the saved file, we found 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 to verify it.

Content-Type:text/json

Content-Type:text/xml

POST data

To implement a function added by User, the accepted type is User entity, and the data of our POST is the corresponding JSON data. See if the problem encountered by dudu in Beta version has been solved.

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}

Let's still use Fiddler to simulate POST data.

Before the POST request, we append the code to the process and set a breakpoint at the Add method.

In Visual Studio 2012, debug HOST's program became IIS Express.

We use Ctrl+ALT+P to attach to its process.

Let's use Fiddler to simulate POST.

Note that the Content-Type in Request Header is text/json,POST and the json content is:

1 {"UserID": 4, "UserName": "Parry", "UserEmail": Parry@cnblogs.com}

After clicking Execute, we jump to the breakpoint we set earlier, and we take a look at the submitted data.

So the problem encountered by dudu in Beta has been solved.

Thank you for reading this article carefully. I hope the article "how to use ASP.NET Web API" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report