Introduction
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:
- Attribute Routing.
- IHttpActionResult.
- Web API OData.
- Cors- Cross origin resource sharing.
- 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:
"config.MapHttpAttributeRoutes();"
The entire code of the WebAPiConfig.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace WebApplication2
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
-
IHttepActionResult:
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:
public IHttpActionResult GetCustomer(int id)
{
var customer = customers.FirstOrDefault((p) => p.ID == id);
if (customer == null)
{
return NotFound();
}
return Ok(customer);
}
-
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.