I already has discussed that ASP.NET Web API is a framework that simplifies the creation of HTTP services. We can build loosely coupled services as Web API follows REST architecture. Another advantage of using HTTP services is that it can be consumed by a wide range of clients.As we are going to perform CRUD (Create, Read, Update, Delete) operations in this Web Development article using HTTP services, we must understand that these how these operations map to basic HTTP verbs.
- Create -> POST
- Read -> GET
- Update -> PUT
- Delete -> DELETE
In order to get start with coding, please create a new ASP.NET MVC 4 Project using Visual Studio and choose Web API template. When the new project is created successfully, you can easily find “Model”, “View” and “Controller” folders inside it.
First of all, we will be creating a new domain model class inside the model folder say “Student.cs” as:
For a better and clean separation, we will add another class “StudentRepository.cs” which will actually perform the CRUD operations. For the purpose of simplicity, I am not going to write complete database interaction code here. You can have implementation of your choice, for example, LINQ or ADO.NET Entity Framework etc.
Now, its time to add controller class to your project. In controller folder, you will find two controller classes by default i.e. HomeController.cs and ValuesController.cs. Add a new controller “StudentsController.cs” under “Controller” folder. Following will be the code for it.
In this ASP.NET Web API Tutorial, we have completed the code for performing CRUD operations using ASP.NET Web API. In second part of this article, we will focus on writing the code for consuming the service.
No comments:
Post a Comment