Click here to Skip to main content
15,890,579 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
28 Dec 2013CPOL2 min read 12.4K   9  
How to perform CRUD operations using ASP.NET Web API - Part 1

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 have already 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 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:

C#
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 the implementation of your choice, for example, LINQ or ADO.NET Entity Framework, etc.

C#
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(stringstudentID)
  {
     //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, it's 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.

C#
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 the second part of this article, we will focus on writing the code for consuming the service.

Recommended Web Development Articles

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
-- There are no messages in this forum --