Debug RESTful Web API Application on REST Client

Introduction

This article explains the REST Client Debugger. It is used for debugging the RESTful Services of a Web  API application. We can download it from this link "https://addons.mozilla.org/en-US/firefox/addon/restclient/" for Mozilla Firebox.

In this article, we will create a Web API Application with Get, Post, Delete operations. Now we can see how to create an application and how to execute it on the REST Client.

Step 1

Create an application using the following:

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • Select "Installed" -> "Templates" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and then 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 we will add two model classes in the project, the first is "Employee" and the other is "EmployeeRepository".

  • In the Solution Explorer.
  • Right-click on the Model Folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#"-> and select "Class".

    Add model class

  • Click on the "OK" button.

In the "Employee.cs" file add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MvcApplication2.Models  
  6. {  
  7.     public class Employee  
  8.     {  
  9.         public int Id { getset; }  
  10.         public string FirstName { getset; }  
  11.         public string LastName { getset; }  
  12.     }  
  13. } 

In the "Employee.Repository" file add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MvcApplication2.Models  
  6. {  
  7.     public class EmployeeRepository:IEmployeeRepository  
  8.     {  
  9.         private List<Employee> _people = new List<Employee>();  
  10.      private int _fakeDatabaseID = 1;  
  11.         public EmployeeRepository()  
  12.         {  
  13.             // For the moment, we will load some sample data during initialization.   
  14.             this.Add(new Employee { LastName = "Jain", FirstName = "Tanya" });  
  15.             this.Add(new Employee { LastName = "Mac, FirstName = "John" });  
  16.             this.Add(new Employee { LastName = "Patel", FirstName = "Smith" });  
  17.             this.Add(new Employee { LastName = "Kohli", FirstName = "Virat" });  
  18.         }  
  19.         public IEnumerable<Employee> GetAll()  
  20.         {  
  21.             return _people;  
  22.         }  
  23.         public Employee Get(int id)  
  24.         {  
  25.             return _people.Find(p => p.Id == id);  
  26.         }  
  27.         public Employee Add(Employee employee)  
  28.         {  
  29.             if (employee == null)  
  30.             {  
  31.                 throw new ArgumentNullException("employee");  
  32.             }  
  33.             employee.Id = _fakeDatabaseID++;  
  34.             _people.Add(employee);  
  35.             return employee;  
  36.         }  
  37.         public void Remove(int id)  
  38.         {  
  39.             _people.RemoveAll(p => p.Id == id);  
  40.         }  
  41.         public bool Update(Employee employee)  
  42.         {  
  43.             if (employee == null)  
  44.             {  
  45.                 throw new ArgumentNullException("employee");  
  46.             }   
  47.             int index = _people.FindIndex(p => p.Id == employee.Id);  
  48.             if (index == -1)  
  49.             {  
  50.                 return false;  
  51.             }  
  52.             _people.RemoveAt(index);  
  53.             _people.Add(employee);  
  54.             return true;   
  55.         }  
  56.     }  
  57. } 

Step 3

Add an interface to the project:

  • In the Solution Explorer.
  • Right-click on the Model Folder.
  • Select "Add" -> "New Item".
  • Select "Installed" -> "Visual C#"-> and select Interface.

    Add interface

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace MvcApplication2.Models  
  7. {  
  8.     public interface IEmployeeRepository  
  9.     {  
  10.         IEnumerable<Employee> GetAll();  
  11.         Employee Get(int id);  
  12.         Employee Add(Employee employee);  
  13.         void Remove(int id);  
  14.         bool Update(Employee employee);  
  15.     }  
  16.  

Step 4

Now add an API Controller to the project:

  • In the "Solution Explorer".
  • Right-click on the "Controller folder".
  • Select "Add" -> "Controller".
  • Select "API Controller" from the template.

    Add api controller

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using MvcApplication2.Models;  
  8. namespace MvcApplication2.Controllers  
  9. {  
  10.     public class EmployeeController : ApiController  
  11.     {  
  12.         static readonly IEmployeeRepository databasePlaceholder = new EmployeeRepository();  
  13.         public IEnumerable<Employee> GetAllPeople()  
  14.         {  
  15.             return databasePlaceholder.GetAll();  
  16.         }  
  17.         public Employee GetEmployeeByID(int id)  
  18.         {  
  19.             Employee employee = databasePlaceholder.Get(id);  
  20.             if (employee == null)  
  21.             {  
  22.                 throw new HttpResponseException(HttpStatusCode.NotFound);  
  23.             }  
  24.             return employee;  
  25.         }  
  26.         public HttpResponseMessage PostEmployee(Employee employee)  
  27.         {  
  28.             employee = databasePlaceholder.Add(employee);  
  29.             var response = this.Request.CreateResponse<Employee>(HttpStatusCode.Created, employee);  
  30.             string uri = Url.Link("DefaultApi" ,new { id = employee.Id });  
  31.             response.Headers.Location = new Uri(uri);  
  32.             return response;  
  33.         }  
  34.         public bool PutEmployee(Employee employee)  
  35.         {  
  36.             if (!databasePlaceholder.Update(employee))  
  37.             {  
  38.                 throw new HttpResponseException(HttpStatusCode.NotFound);  
  39.             }  
  40.             return true;  
  41.         }  
  42.         public void DeleteEmployee(int id)  
  43.         {  
  44.             Employee employee = databasePlaceholder.Get(id);  
  45.             if (employee == null)  
  46.             {  
  47.                 throw new HttpResponseException(HttpStatusCode.NotFound);  
  48.             }  
  49.             databasePlaceholder.Remove(id);  
  50.         }  
  51.     }  
  52. } 
Step 5

Execute the application, then copy the URL. And after downloading the REST Client we can see that in Firebox there is an Icon display.

Successfully Execute

REST Client Icon

We click on that icon and paste the URL and select the operation.

Display All employee

Click on the Response body (preview) and see the results:

Responce body Preview


Similar Articles