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

By | January 14, 2014
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

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.

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:

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

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.

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

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

  • Dorababu Meka

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

  • See my reply above.

    • 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

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

  • Nimi

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

  • 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

  • dillesu

    nice ariticals

  • 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