当前位置:首页 > Windows程序 > 正文

Using ASP.Net WebAPI with Web Forms

2021-03-29 Windows程序

Asp.Net WebAPI is a framework for building RESTful HTTP services which can be used across a wide range of clients including web, phone and desktop applications. With WebAPI we can use XML and JSON to send and retrieve data from the service. The use of Json or XML makes it quite flexible to be used across a variety of devices. 

Although WebAPI ships and installs with ASP.Net MVC 4, it can be used in normal Web Forms application as well.

In this example, we will use WebAPI with a traditional Web Forms application for adding and displaying a list of User Information. We will be using Visual Studio 2012 and .Net 4.5. 

Creating the Project

Open Visual Studio 2012, Go To New -> Project. Select Visual C# -> Web from the left hand navigation and select project type as ASP.Net Web Forms Application. Enter the project name as WebAPIDemo

技术分享

A new project will be created with a default template and our solution will show the following structure

技术分享

Adding Model Class

We will then add a Model Class to our project. The Model class will represent the data in our application. We will add a model class called Users in our project

Right Click the Project in the Solution Explorer, Select Add -> Class from the Context Menu. The name of the class will be “Users” and it will have the following properties

技术分享

Hide   Copy Code

namespace WebAPIDemo { public class Users { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Company { get; set; } public string Email { get; set; } public string PhoneNo { get; set; } } }

The WebAPI can automatically serialize the model as JSON or XML object and will write the data in the body of the HTTP response. If the clients can read the serialize data, they will be able to deserialize it. Most of the clients are able to read the XML or JSON data. The clients can also specify the format it wants by setting the “Accept Header” property in the request message.

Creating The Repository

Ideally in a production scenario, we should be storing our data in some sort of persistent storage like database but to keep this demo simple, we will storing it in local memory.

To keep our storage implementation separate from the application code, we will be using Repository pattern. This way we can change our storage implementation at any future data without many changes to the application code.

We will start by creating a generic repository interface. Add a new class to the project and call it“IUserRepository”. It will have the following code

Hide   Copy Code

namespace WebAPIDemo { public interface IUserRepository { IEnumerable<Users> GetAllUsers(); string AddUser(Users user); Users GetUserById(int id); void DeleteUser(int id); } }

The “GetAllUsers” function return a list of the all the users. The “AddUser” function will add a new user and“GetUserById” will return a particular user based on the id that has been passed.

Add a Class to the solution called “UserRepository”. The class will implement the “IUserRepository” and will have the following implementation.

Hide   Shrink    Copy Code

namespace WebAPIDemo { public class UserRepository : IUserRepository { private List&lt;Users&gt; userList = new List&lt;Users&gt;(); private int _id = 3; public UserRepository() { userList.Add(new Users { Id = 1, FirstName = "Madhur", LastName = "Kapoor", Company = "ABC", Email = "madhur@abc.com", PhoneNo = "65431546" }); userList.Add(new Users { Id = 2, FirstName = "Alan", LastName = "Wake", Company = "XYZ Corp", Email = "alan@xyz.com", PhoneNo = "64649879" }); } public IEnumerable&lt;Users&gt; GetAllUsers() { return userList; } public string AddUser(Users user) { user.Id = _id++; userList.Add(user); return "User added"; } public Users GetUserById(int id) { var user = userList.FirstOrDefault&lt;Users&gt;((p) =&gt; p.Id == id); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return user; } public void DeleteUser(int id) { var user = userList.FirstOrDefault<users>((p) => p.Id == id); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } userList.Remove(user); } }</users>

In the class, we are using a local variable “userList” to store all the users. In the constructor, we are adding a few records to the list.

The “AddUser” function adds a new user to the list with a new Id.

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69708.html