Performing CRUD operations using ASP.NET Web API – Part 1

By | December 27, 2013
In one of my previous ASP.NET Web API Tutorial, I explained 3 simple steps to create your first ASP.NET Web API service. In this article, I’ll try to perform all CRUD (Create, Read, Update, Delete) operations using Microsoft ASP.NET Web API.ASP.NET Web API

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.


Please look into following piece of code:

[ActionName(“GetById”)]

public ActionResult GetEmployeeById()
{
//Code logic here.

return View();
}
What’s true about the above code.

  • A. “GetEmployeeById” action method with be identified and called by name “GetEmployeeById”.
  • B. “GetEmployeeById” action method with be identified and called by name “GetById”.
  • C. Above code will generate an error because of wrong return type.
  • D. Action method can’t be called because of duplicate action method names.

For a complete ASP.NET MVC online test and Practice Exams, Click Here.

 Correct Answers: B

Other Related Articles:

Top 10 Interview Questions and Answers Series:

12 thoughts on “Performing CRUD operations using ASP.NET Web API – Part 1

  1. Braj Panda

    Nice post. By the way, I would like to know how you got those code blocks in that grey box. Can you please share the trick? Thanks in advance.

  2. WebdevTutorial

    Your is very nice article about Performing CRUD operations using ASP.NET Web API.
    Can you please share the client code to make call to webAPI.
    Thanks
    Shrirang

    1. WebdevTutorial

      Dear Shrirang,
      Thanks for your appreciation.
      Part 2 of this tutorial is about consuming ASP.NET Web API with client code.

      Thanks
      Imran

  3. WebdevTutorial

    I believe you are making some mistake while calling a method. This type of error occurs normally when someone calls a method at class level instead of making call inside a method.
    If unable to resolve, please share the code where you’r getting this error.

    1. swapna

      hello,
      ur explanation is really very good,my code got executed..thanx
      hope you will respond to my further queries !!

  4. WebdevTutorial

    This error clearly indicates that “Product” is inaccessible in your code. Please make sure that your model “Product” is accessible in controller code? Verify the namespaces if you explicitly have given?

  5. Divya MK

    Your tutorial is very nice.
    Can you please share the source code,implementing any of the method in ‘StudentRepository’ class.I am confused on how to implement CRUD operation using linq or ADO.NET entity framework

  6. Divya MK

    Your tutorial is very nice.
    Can you please share the source code,implementing any of the method in ‘StudentRepository’ class.I am confused on how to implement CRUD operation using linq or ADO.NET entity framework

  7. Cipher Coder

    I want to see some articles explaining CRUD in asp .net Web API 2 using angular 2, in the same lucid and beautiful manner as this article is.. so.. which one ..?

Comments are closed.