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

By | December 27, 2013
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
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

You can follow here to get a comprehensive list of FAQs or ASP.NET Web API Interview Questions. This Web API Tutorial is designed to grasp the core Web API Concepts with few tricky Interview Questions List.

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:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
Category: ASP.NET Web API Tags:

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several 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 www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.

  • 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.

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

  • 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

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

      Thanks
      Imran

  • 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 !!

  • 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?

  • 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

  • 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