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

By | December 27, 2013

In one of my previous articles, 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.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:

 public class Student
 {
          public string StudentID { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
 }

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.

 public class StudentRepository
 {
         private static List<Student> students;
         
         public static List<Student> GetAllStudents()
         {
                   //Code logic to get all students.
          }
          public static Student GetStudent(string studentID)
         {
                  //Code Logic to get all students.
          }
          public static void RemoveStudent(string studentID)
          {
                  //Code Logic to delete a student
          }
           public static void AddStudent(Student student)
           {
                  //Code Logic to Add a new student.
            }
            public static void UpdateStudent(Student student)
            {
                  //Code Logic to Update a student.
            }

  }
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.
 public class StudentsController : ApiController
 {
          public List<Student> Get()
          {
                  return StudentRepository.GetAllStudents();
          }
           public Student Get(string id)
           {
                   return StudentRepository.GetStudent(id);
           }
           public void Post(Student Student)
           {
                   StudentRepository.AddStudent(Student);
            }
            public void Put(Student Student)
            {
                   StudentRepository.UpdateStudent(Student);
            }
             public void Delete(string id)
             {
                    StudentRepository.RemoveStudent(id);
             }
 }

In this ASP.NET Web API article 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.

Top 10 Interview Questions and Answers Series:

  • http://www.blogger.com/profile/06536011829537447138 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.

    • http://www.blogger.com/profile/10867674357364468276 Imran Ghani

      Thanks Braj. In order to gray code box, you need to understand HTML. I modified the HTML.

  • http://www.webdevelopmenthelp.net/ 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

    • http://www.webdevelopmenthelp.net/ WebdevTutorial

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

      Thanks
      Imran

  • http://www.webdevelopmenthelp.net/ 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.

    • swapna

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

  • http://www.webdevelopmenthelp.net/ 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?