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:

Step 2

Now add the ADO.NET Entity Model.

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:

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