Identifying Resources in ASP.NET Web API | ASP.NET Web AP| Programmer Guide | ASP.NET Web API Tutorial

In previous articles, we have discussed about what is RESTful service and the constraints involved and how to resolve these constraints of REST in ASP.Net Web API. Now let us see how to identify a resource in ASP.Net Web API?.

A resource is any data that a web API sends to its clients. Examples could be a product that your company sells, a purchase order received from a buyer, a list of employees in your company, or an individual employee in a department. In the real world, a product or an employee could be uniquely identified through an identifier, such as a product ID or an employee ID.

In the case of RESTful web services, a resource is identified by a URI. An employee with an identifier of 12345 will be represented by http://server/employees/12345. In the case of ASP.NET Web API, the URI can be slightly different and it includes api by default in the URI, so it will be more like http://server/api/employees/12345. If you fire up an instance of Internet Explorer, type that URI in the address bar, and press Enter, Internet Explorer does an HTTP GET and you will get the JSON representation of the resource, which is an employee with the ID of 12345 in this case.

From the .NET code point of view (see code below), the corresponding class will be EmployeesController, which is a subclass of ApiController and the method that executes to create the resource representation to be sent back to the client in its Get(int) method.

public class EmployeesController : ApiController
    {
        public Employee Get(int id)
        {
            // return employee      
        }
        public IEnumerable<Employee> GetAllEmployees()
        {
            // return all employees  
        }
    }
In the above code, the resource that is a noun has the URI representation of http://server/api/employees/12345. This resource was accessed through GET HTTP method, which is the verb. Like one single employee, a list of employees is also a resource and its identifier will be http://server/api/employees. The corresponding method is GetAllEmployees(), which returns IEnumerable<Employee>.