REST Service in ASP.NET Web API

Introduction

Representational State Transfer (REST) is a service based on REST architecture. We can easily create REST services that can be used from other devices. The principles apply more on the web and HTTP. We can also use its principles in other distributed systems. In the worldwide web, there is an implementation of the REST architecture. We can easily implement the architecture on the other services. A simple service created using the REST architecture is called a REST service.

When we create the REST services it is important that we follow the rules of the HTTP protocol. Various APIs of various languages create and use the REST services. If we do not know the principle of REST then we can create the service but the service will work in the RPC style of service. So in this article, we will learn how to create a REST service.

REST

The REST architecture is based on the client and server. The client and server communicate with each other. It is based on the resource and request of the client and server and REST uses a uniform interface. In it, the client accesses the server's resource and the server returns the resource according to the client-based request. The resource can be accessed by the address called a URI Address.

Architecture of REST

Architecture of REST

What is a Uniform Interface?

Uniform Interface is an important feature of REST. It has a group of methods; they are GET, PUT, POST, DELETE, etc. These methods are easily understandable by the client and the server. It is important to use the correct method for the specified action. If the request is based on the GET method and it chooses the DELETE method then the resource will be DELETEd. That would be a problem for the other methods that perform the appropriate action.

What is ASP.NET Web API?

The ASP.Net Web API has a controller that is called the API controller. For developing MVC there are using two types of controllers.

The first one is the API controller and the second one is the default MVC controller. It is important to choose the correct controller for the specified action. On the creation of the REST service, we use the API controller. These controllers return the data. We use the default controller for returning the views.

Sample for creating the simple service

We provide a sample for creating a simple service that manages all the tasks. Before creating it we need to identify the resources of the application and select the right method and address related to resources.

here we provide an example of a mapping table.

Action Method URI
Get all the Task GET /tasks
Get a single method GET /tasks/id
Create a new task POST /tasks
Edit a task PUT /task/id
Delete a task DELETE /task/id

Now we create a new ASP.Net MVC4 web application.

MVC4 web application

We select the empty template for the creation of the MVC4 application.

MVC4 application

In the Global.asax.cs. there are two routes defined; the first is MVC controller and the second is API controller, as in the following.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapHttpRoute(
        name: "default_Api",
        routeTemplate: "api/{controller}/{t_id}",
        defaults: new { id = RouteParameter.Optional }
    );

    routes.MapRoute(
        name: "default_app",
        url: "{controller}/{action}/{t_id}",
        defaults: new { controller = "home", action = "Index", t_id = UrlParameter.Optional }
    );
}

Now we create a service that removes the data and correspondingly modifies the other data, as in the following.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapHttpRoute(
        name: "Default_app",
        routeTemplate: "{controller}/{t_id}",
        defaults: new { t_id = RouteParameter.Optional }
    );
}

Now we create a resource task1, as in the following.

public class Task1
{
    public int T_Id { get; set; }
    public string T_Description { get; set; }
    public int T_Priority { get; set; }
    public DateTime Created { get; set; }
}

We create the controller that exposes various methods that perform the resource, as in the following.

public class Task_Control : ApiController
{
    public IEnumerable<string> Get()
    {
        return new string[] { "val1", "val" };
    }

    public string Get(int t_id)
    {
        return "val";
    }

    public void Post(string val)
    {
    }

    public void Put(int t_id, string val)
    {
    }
}

In this code, we can see that the class Task_Control is derived from ApiController. It has a method for Getting all the tasks, Getting a single task, creating a new task, and editing the task.

now we describe the return type of method.

public class Task_Control : ApiController
{
    public IEnumerable<Task> Get()
    {
        throw new NotImplementedException();
    }

    public Task Get(int t_id)
    {
        throw new NotImplementedException();
    }

    public HttpResponseMessage<Task> Post(Task t_task)
    {
        throw new NotImplementedException();
    }

    public Task Put(Task task)
    {
        throw new NotImplementedException();
    }
}

When we implement the task, first we need to store our task.

public interface ITaskRepository
{
    IEnumerable<Task> Get();
    Task Get(int taskId);
    Task Post(Task task);
    Task Put(Task task);
}

public class TaskRepository : ITaskRepository
{
    private List<Task> Tasks
    {
        get
        {
            if (HttpContext.Current.Cache["Tasks"] == null)
                HttpContext.Current.Cache["Tasks"] = new List<Task>();
            return HttpContext.Current.Cache["Tasks"] as List<Task>;
        }
        set
        {
            HttpContext.Current.Cache["Tasks"] = value;
        }
    }

    public IEnumerable<Task> Get()
    {
        return Tasks;
    }

    public Task Get(int taskId)
    {
        return Tasks.Find(p => p.TaskId == taskId);
    }

    public Task Post(Task task)
    {
        task.TaskId = Tasks.Max(t => t.TaskId) + 1;
        Tasks.Add(task);
        return task;
    }

    public Task Put(Task updatedTask)
    {
        var existingTask = Get(updatedTask.TaskId);
        if (existingTask == null)
            throw new Exception("The Task does not exist.");
        existingTask.Description = updatedTask.Description;
        existingTask.Priority = updatedTask.Priority;
        return existingTask;
    }
}

In this code, Task_Repos is a class that is derived from the ITask_Repos interface. In it, we use the HttpContext.Current.A cache that stores the task.

private readonly ITask_Repos _task_Repos;

public Tasks_Contrl(ITask_Repos task_Repos)
{
    _task_Repos = task_Repos;
}

GET() Method

This method returns all the tasks.

public IEnumerable<Task> Get()
{
    return _task_Repos.Get();
}

GET(T_id) Method

This method is used for a single variable.

public Task Get(int t_id)
{
    var t_task = _task_Repos.Get(t_id);
    if (t_task == null)
    {
        throw new HttpResponseException(new HttpResponseMessage
        {
            StatusCode = HttpStatusCode.NotFound,
            Content = new StringContent("This task is not available")
        });
    }

    return t_task;
}

Post(Task Task) Method

This method adds a resource.

public async Task<HttpResponseMessage> Post(Task task)
{
    var createdTask = _task_Repos.Post(task);

    var response = new HttpResponseMessage(HttpStatusCode.Created)
    {
        Content = new ObjectContent<Task>(createdTask, new JsonMediaTypeFormatter())
    };

    string uri = Url.Route(null, new { id = createdTask.t_Id });
    response.Headers.Location = new Uri(Request.RequestUri, uri);

    return response;
}