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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2.Models
{
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
In the "Employee.Repository" file add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2.Models
{
public class EmployeeRepository:IEmployeeRepository
{
private List<Employee> _people = new List<Employee>();
private int _fakeDatabaseID = 1;
public EmployeeRepository()
{
// For the moment, we will load some sample data during initialization.
this.Add(new Employee { LastName = "Jain", FirstName = "Tanya" });
this.Add(new Employee { LastName = "Mac, FirstName = "John" });
this.Add(new Employee { LastName = "Patel", FirstName = "Smith" });
this.Add(new Employee { LastName = "Kohli", FirstName = "Virat" });
}
public IEnumerable<Employee> GetAll()
{
return _people;
}
public Employee Get(int id)
{
return _people.Find(p => p.Id == id);
}
public Employee Add(Employee employee)
{
if (employee == null)
{
throw new ArgumentNullException("employee");
}
employee.Id = _fakeDatabaseID++;
_people.Add(employee);
return employee;
}
public void Remove(int id)
{
_people.RemoveAll(p => p.Id == id);
}
public bool Update(Employee employee)
{
if (employee == null)
{
throw new ArgumentNullException("employee");
}
int index = _people.FindIndex(p => p.Id == employee.Id);
if (index == -1)
{
return false;
}
_people.RemoveAt(index);
_people.Add(employee);
return true;
}
}
}
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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MvcApplication2.Models
{
public interface IEmployeeRepository
{
IEnumerable<Employee> GetAll();
Employee Get(int id);
Employee Add(Employee employee);
void Remove(int id);
bool Update(Employee employee);
}
}
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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class EmployeeController : ApiController
{
static readonly IEmployeeRepository databasePlaceholder = new EmployeeRepository();
public IEnumerable<Employee> GetAllPeople()
{
return databasePlaceholder.GetAll();
}
public Employee GetEmployeeByID(int id)
{
Employee employee = databasePlaceholder.Get(id);
if (employee == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return employee;
}
public HttpResponseMessage PostEmployee(Employee employee)
{
employee = databasePlaceholder.Add(employee);
var response = this.Request.CreateResponse<Employee>(HttpStatusCode.Created, employee);
string uri = Url.Link("DefaultApi" ,new { id = employee.Id });
response.Headers.Location = new Uri(uri);
return response;
}
public bool PutEmployee(Employee employee)
{
if (!databasePlaceholder.Update(employee))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return true;
}
public void DeleteEmployee(int id)
{
Employee employee = databasePlaceholder.Get(id);
if (employee == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
databasePlaceholder.Remove(id);
}
}
}
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]()