[Web API] Overview

Web API is a new framework to provide services through HTTP in a RESTful manner. Unless you’re stuck with .NET 3.5 or SOAP based services, you need to choose Web API over WCF.

 

1. Web API

Web API is not a part of ASP.NET MVC. Web API assemblies are available through NuGet packages. Therefore, you can use it anywhere. Web API is basically built on top of ASP.NET but self-hosting (Windows Services or desktop applications) is also available.

The main assembly of Web API is “System.Web.Http“. If you create a new MVC application, Web API is included automatically. For clients, “System.Net.Http” namespace is used.

 

2. Controller

Web API uses controllers to process requests. But it is different from MVC controllers.

  • It is derived from “ApiController” class that implements “IHttpController” interface.
  • It does not need to be located in the “Controllers” folder. It can be located anywhere in the project. Many developers use the “API” folder.
public abstract class ApiController : IHttpController
{
  public HttpRequestMessage Request { get; set; }
  public HttpConfiguration Configuration { get; set; }
  public HttpControllerContext ControllerContext { get; set; }
  public ModelStateDictionary ModelState { get; }
  public IPrincipal User { get; }
  public UrlHelper Url { get; set; }
}

public class ProductController : ApiController
{
}

 

3. Configuration

Web API provides a simple and centralized configuration Model using the “System.Web.Http.HttpConfiguration” class.

If you are hosting Web API in the ASP.NET application, you can access this object through the static “GlobalConfiguration.Configuration” property anywhere in the application.

public static class GlobalConfiguration
{
  public static HttpConfiguration Configuration { get; }
  public static HttpMessageHandler DefaultHandler { get; }
}

 

4. Routing

The most unique feature of Web API is routing. Web API is based on the resources.

  • Routes by HTTP verbs (GET, POST, PUT, DELETE)
  • Action names match with the HTTP verbs.

Routing is configured using the “HttpConfiguration.Routes.MapHttpRoute()” method rather than “Routes.MapRoute()“.

public static void Register(HttpConfiguration config)
{
  config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
  );
}

Note that there is no {action} in the route.

 

5. Restful Services

Web API uses HTTP verbs to specify its CRUD actions:

  • GET: Read
  • POST: Create
  • PUT: Update
  • DELETE: Delete

[Note] Do not confuse “POST” with “PUT“.

 

6. Allowing HTTP Verbs

In some cases, web hosts do not allow all HTTP verbs.

One example is IIS Express with Visual Studio. To allow more verbs:

  • Go to the “IISExpressconfig” under “My Documents”
  • Open the “applicationhost.config” file
  • Find the “ExtensionlessUrl-Integrated-4.0” app setting
  • Add more verbs; by default ‘GET,HEAD,POST,DEBUG’ are allowed.

In IIS, you can go to the “Handler Mappings” in your IIS Manager. Find “ExtensionlessUrlHandler-Integrated-4.0”.

<add name="ExtensionlessUrl-Integrated-4.0"
  path="*."
  verb="GET,HEAD,POST,DEBUG,PUT,DELETE"
  type="System.Web.Handlers.TransferRequestHandler"
  preCondition="integratedMode,runtimeVersionv4.0"
  responseBufferLimit="0" />

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s