CRUD Operations in ASP.Net Web API Using Fiddler Tool

Introduction

In this article we will do Create, Read, Update and Delete (CRUD) operations in the WebAPI using the Fiddler Tool. Here we need to create a database and connect with the Web API application. We can use the various methods for the CRUD operations in Fiddler, such as POST, GET, PUT and DELETE. The POST method is for creating the data, GET is used to read the data, PUT is used to update the data and finally is the DELETE method for deleting the data.

Now let's start by creating the database in SQL.

Step 1

Create a database :

  1. use mudita  
  2. create table UserInfo(UId varchar(10) primary key, UserName varchar(20), Address varchar(50))  
  3. Insert into UserInfo values('1','Smith','Delhi')  
  4. Insert into UserInfo values('2','Jhon','Gurgao')  
  5. Insert into UserInfo values('3','Priyam','Manipur') 

Step 2

Now we will create a Web API application:

  • Start Visual Studio 2013.
  • From Start window Select "New Project".
  • Select "Installed" -> "Template" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and select "ASP.NET MVC4 Web Application".

    Select MVC4 application

  • Click on the "OK" button.
  • From the MVC4 project window select "Web API".

    Select Web API

  • Click on the "OK" button.

Step 2

Now add the ADO.NET Entity Model.

  • Right-click on the Model folder in Solution Explorer.
  • Select "Add" -> "New Item" -> "ADO.NET" and click on the "Add" button.

    Add ADO.NET Model

  • Open a new Entity wizard.

    Entity Model Wizard

  • Click on the "Next" button then open the new dialog box.
  • Click on the "New connection" button.
  • And fill in the user id and password.

    Create new connection with user id and password

  • Now select the option to create the sensitive data.
  • Click on the "Next" button.
  • Now select the table.

    Selection Option

  • Click on the "Finish" button.
  • Then the Model diagram is displayed.
    Diagram of table

Step 3

Now in the Model folder add the Repository class with the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace CURDFiddler.Models  
  6. {  
  7.     public class UsersRepository  
  8.     {  
  9.         public static List<UserInfo> GetAllUsers()  
  10.         {  
  11.             MuditaEntities1 dataContext=new MuditaEntities1();  
  12.             var query = from user in dataContext.UserInfoes  
  13.                         select user;  
  14.             return query.ToList();  
  15.         }  
  16.         public static UserInfo GetUser(string userID)  
  17.         {  
  18.             MuditaEntities1 dataContext=new MuditaEntities1();  
  19.             var query = (from user in dataContext.UserInfoes  
  20.                          where user.UId==userID  
  21.                          select user).SingleOrDefault();  
  22.             return query;  
  23.         }  
  24.         public static void InsertUser(UserInfo newUser)  
  25.         {  
  26.             MuditaEntities1 dataContext = new MuditaEntities1();  
  27.             dataContext.UserInfoes.Add(newUser);  
  28.             dataContext.SaveChanges();  
  29.         }  
  30.         public static void UpdateUser(UserInfo oldUser)  
  31.         {  
  32.             MuditaEntities1 dataContext = new MuditaEntities1();  
  33.             var query = (from user in dataContext.UserInfoes  
  34.                          where user.UId==oldUser.UId  
  35.                          select user).SingleOrDefault();  
  36.             query.UId= oldUser.UId;  
  37.             query.UserName = oldUser.UserName;  
  38.             query.Address = oldUser.Address;  
  39.             dataContext.SaveChanges();  
  40.         }  
  41.         public static void DeleteUser(string UId)  
  42.         {  
  43.             MuditaEntities1 dataContext = new MuditaEntities1();  
  44.             var query = (from user in dataContext.UserInfoes  
  45.                          where user.UId == UId  
  46.                          select user).SingleOrDefault();  
  47.             dataContext.UserInfoes.Remove(query);  
  48.             dataContext.SaveChanges();  
  49.         }  
  50.     }  
  51. }   

Step 4

In the Controller Folder add the API controller:

  • In the Solution Explorer.
  • Right-click on the Controller folder.
  • Select "Add" -> "Controller".
  • From the Template select "Empty API controller".

    API Controller

  • Click on the "OK" button.

Add the following code:

  1. using CURDFiddler.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. namespace CURDFiddler.Controllers  
  9. {  
  10.     public class UsersController : ApiController  
  11.     {  
  12.         public List<UserInfo> Get()  
  13.         {  
  14.             return UsersRepository.GetAllUsers();  
  15.         }  
  16.         public UserInfo Get(string id)  
  17.         {  
  18.             return UsersRepository.GetUser(id);  
  19.         }  
  20.         public void Post([FromBody]UserInfo user)  
  21.         {  
  22.             UsersRepository.InsertUser(user);  
  23.         }  
  24.         public void Put([FromBody]UserInfo user)  
  25.         {  
  26.             UsersRepository.UpdateUser(user);  
  27.         }  
  28.         public void Delete(string id)  
  29.         {  
  30.             UsersRepository.DeleteUser(id);  
  31.         }  
  32.     }  
  33. }   

Step 5

  • Execute the application. Copy the URL. Now open Fiddler and click on the compose tab and then paste the URL and navigate to the following URL: "http://localhost:18849/api/users".
    Fetch data from database

    Now fetch the data from the database through ID:

    Fetch record by ID

  • Now we insert the data in the database. Select the POST method then write the URI into the address bar. And then specify the Content-Type:application/json. Use the following code for the Request body:

    {"UId":"4","UserName":"Priya","Address":"Lucknow"}

    Insert Data

    Click on the "Execute" button and again select the GET method for showing the data.

    Post Data Complete

  • Update the data in the database. Select the PUT method then write the URI into the address bar. then Specify the Content-Type:application/json. Use the following code for the request body:

    {"UId":"3","UserName":"Vikrant","Address":"Chandighar"}

    Updating data with using Put method

    Click on the "Execute" button and again select the GET method for displaying the data.

    Updation of Data in Database

  • Now to delete the data from the database, select the delete method and write the URI with the ID.

    Select Delete Method

  • Click on the "Execute" button and again see the data.

    Record Deleted


Similar Articles