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

By | January 14, 2014

In part-1 of this Web API development tutorial, we developed an application that perform all CRUD (Create, Retrieve, Update, Delete) operations using Microsoft ASP.NET Web API. Now, In this part, we will consume HTTP service developed using ASP.NET Web API using jQuery.ASP.NET Web API

If you haven’t gone through first part of this article, I’ll recommend to read and understand Part-1 and then move to this part.
I am going to recap what we implemented in Part-1 i.e. How HTTP service is developed using ASP.NET Web API.

  • Created a domain model class i.e. Student
  • Implemented a StudentRepository class that contains actual code for DB interaction.
  • Added a StudentController class implementing ApiController.

Focus of this ASP.NET Web API Tutorial will remain on jQuery code for consuming HTTP service, although following is the HTML code for displaying data returned from Web API service on get requrest. You can add following HTML table to your web page.

jQuery Ajax call for all CRUD operations has following important elements that need to be understood for implementation.

  • type is HTTP verb used for the calls i.e. GET, POST, PUT, DELETE etc.
  • url is the Web API service URL pointing to Controller class.
  • Content Type is the type of data sending to server i.e. JSON here.
  • dataType is the type of data expected back from server i.e. JSON.

So, in order to get all students and display on a web page, GetAllStudents() function make jQuery ajax call with “GET” as type and url pointing to our Web API service controller i.e. StudentsController.

On success, stringify the JSON object, parse and load into students table as a row for each separate record.

For getting a specific student record we can modify the URL by passing id of student as follows:
    http://localhost/CRUDWebAPI/api/students/1
GetStudentById() doing almost the same as that of GetAllStudents() except that it passes id for fetching the records.

Now, for Adding a new student, AddNewStudent() function does the following:

  • Prepare a JSON object and pass as a parameter to “data” element of jQuery ajax call.
  • type is “POST” for create operation.
  • url is pointing to StudentsController.
For Updating an existing student, UpdateStudent() function does the following:

  • Prepare a JSON object and pass as a parameter to “data” element of jQuery ajax call.
  • type is “PUT” for update operation.
  • url is pointing to StudentsController with StudentId.
Finally, for deleting a record, jQuery ajax call code in DeleteStudent() function is as follows:

  • type is “DELETE” for delete operation.
  • url is pointing to StudentsController with StudentId.
Hopefully this series of articles on ASP.NET Web API Tutorial will be helpful for web developers to understand building and consuming RESTful services. Also, User can also understand more Web API related concepts by following the ASP.NET Web API Interview Quesions or FAQs Series.
Previous : Creating ASP.NET Web API Service – Part 1

Top 10 Interview Questions and Answers Series:

20 thoughts on “Performing CRUD operations using ASP.NET Web API – Part 2

  1. Erick Moises Racancoj Amperez

    It’s a nice article, but i’m lost. In which webpage do i have to add the html code? Do I have to create a view related to the StudentsController? “Focus of this article will remain on jQuery code for consuming HTTP service, although following is the HTML code for displaying data returned from Web API service on get requrest. You can add following HTML table to your web page.” Thanks man…

    1. Imran Ghani

      Dear Erick,
      I have added HTML table just for simplicity. You can render contents from jQuery on the basis of your own requirement, may be, by creating a view but jQuery AJAX call will remain the same.

  2. Dorababu Meka

    Can I have the sample code please, I didn’t get that HTML part

      1. Dorababu Meka

        Thanks Imran, one more help how we can bind a asp:dropdown using the api approach, I need it in asp.net way not mvc way

          1. Dorababu Meka

            That is in MVC I am using asp.net approach, this i need to bind with all student ID’s

          2. WebdevTutorial

            No, Even then you can adjust according to your requirement.
            Anyways, I’ll provide the same above example fulfilling your requirement shortly in an update here.

          3. Dorababu Meka

            I have done that as per the requirement Imran

          4. Dorababu Meka

            Hi I need a favor with in the same class I need to give an option to upload an image so how can I do that

    1. Developer

      Hi thanks I have done this yesterday, but looking for your simplified solution. I have done this in add case but on edit case if I didn’t upload an image and trying to save data is not displaying

  3. shrirang

    I already have looked at part2.. However I did not get how to construct html.
    I want the source of html. I did not get that portion properly and how to construct html. Hence this request.

  4. Nimi

    Great Tutorial! Thank you. Can you give some tips on how to connect to a SQL server?

  5. Bhanu Prakash

    Hi Imraan,
    In web api i connected to sql and it is working fine. I am facing issues in fetching the data from the ajax call.The web api method is firing and the data is returning but it is throwing error while fetching the data.If you share the web api and the client side solution it will be helpful.My mail id is bhanuprakash900@gmail.com .I will be waiting for your reply.

    Regards,
    Bhanuprakash

  6. SARAVANA KUMAR PS

    I changed these ajax call to angular js call using $http({method:’POST’,url:’urlofapi’,data:dataformethod})
    this is not hitting the function in controller, do i miss something here

  7. Harsh Kumar

    nicely explained, easy to understand, must read recommend for all Full stack or UI developers.

Comments are closed.