Features of ASP.Net Web API2


This article explains the features of the ASP.NET Web API2. There are various new features included in the WEB API2. Web API2 is the second version of  the Web API that has some new features. The following is a list of the new features of Web API 2:

  1. Attribute Routing.
  2. IHttpActionResult.
  3. Web API OData.
  4. Cors- Cross origin resource sharing.
  5. OWIN. 
Attribute Routing:

This is a new type of routing support by the Web API2. As the name suggests, it uses the attribute for defining the routes. Routing helps the Web API to match the URI with an action. The first version of the Web API used routing that was called convention-based routing.

For enabling the attribute routing in the application just write this one line in the "WebApiConfig.cs" file:


The entire code of the WebAPiConfig.cs looks like this:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web.Http;  
  5. namespace WebApplication2  
  6. {  
  7.     public static class WebApiConfig  
  8.     {  
  9.         public static void Register(HttpConfiguration config)  
  10.         {  
  11.             // Web API configuration and services  
  12.             // Web API routes  
  13.             config.MapHttpAttributeRoutes();  
  14.             config.Routes.MapHttpRoute(  
  15.                 name: "DefaultApi",  
  16.                 routeTemplate: "api/{controller}/{id}",  
  17.                 defaults: new { id = RouteParameter.Optional }  
  18.             );  
  19.         }  
  20.     }  
  21. }   

The Web API2 is the second version of the Web API. In the Web API there were two ways to create the response, the first one returns a specific object instance, then it is changed to an "HttpResponseMessage" by the Web API pipeline. The other one can return the response as a raw "HttpResponseMessage".

But the Web API2 includes a new way to create the response, "IHttpActionResult". It is the new way of returning the HTTP message from the controller. This feature of the Web API2 increases the reusability and testability of the code.

We can use the IHttpActionResult as in the following:


  1. public IHttpActionResult GetCustomer(int id)  
  2. {  
  3.     var customer = customers.FirstOrDefault((p) => p.ID == id);  
  4.     if (customer == null)  
  5.     {  
  6.         return NotFound();  
  7.     }  
  8.     return Ok(customer);  
  9. }   
WebAPI OData:

OData is a Web protocol that is basically used for querying and updating the data. There are three options supported by the Web API2 for OData, as follows:

  • $Select: Allows the client to include a specific property of the entity in the response.

  • $expand: For including the related entity in the response.

  • $value: For allowing the user to find the raw value of the property.

We use these three options of the OData for controlling the representation.

Cross Origin Resource Sharing (CORS):

CORS supports cross-domain cells. It is the exact technique that allows a web page making an Ajax to call a domain but actually that domain is indicated to a specific web page. Actually browsers do not support CORS but it is now supported by browsers using the WEB API2.

Open Web Interface for .Net (OWIN) self Hosting:

There is an another Selfhosting property introduced by the Web API2. We use it by installing the package "Microsoft.AspNet.Api.OwinSelfHost". OWIN works as an interface. The main work of this interface is that it disassociates the web server from the web application.