Understanding the life of a request in Web API


This article explains requests and the flow of requests, including how a client sends a request to an ASP.NET Web API application.

Request Processing

When a client sends a request to an ASP. NET Web API application there are three layers that process the request. The main components are shown in the following figure.

Let’s see what happens at each stage from the top to the bottom.

The Hosting Layer

The first layer is the hosting layer that receives the HTTP request directly from the client. The hosting layer can be a classic Internet Information Server (IIS) that uses the ASP.Net pipeline or a self-hosted application. The hosting layer plays a very important role in the Web API .Hosting layer. It receives the request from the client and converts it into an instance of the HttpRequestMessage class that contains the request. Then the message is passed to the Message Handler pipeline.

Message Handler Pipeline

The Message Handler pipeline is in the middle of our architecture. It consists of a chain of handlers that are pluggable to meet the needs of the application. Each handler is an instance of the class derived from HttpMessageHandler and it contains the one method SendAsync that receives an instance of HttpRequestMessage and returns a HttpResponseMessage. Each of these handlers has a reference to an InnerHandler, the next handler in the chain that will be called in sequence. As shown in our figure, each request can be pre-processed and post-processed by multiple handlers.

Let us understand the example of a Message Handler pipeline. The HttpRoutingDispatcher that sends the request based on route and HttpControllerDispatcher that sends the request to the controller.

These handlers are already on the chain, since they are in the collection HttpConfiguration.MessageHandlers. Others can be added to the collection during the configuration of the Web API application.

Controller Handling

Now the last level in our figure. In this layer the controller handling accepts the request from the Message Handler Pipeline and it calls the action inside the controller.

The task is intact by the HttpControllerDispatcher, the last handler in the chain. HttpControllerDispatcher obtains the instance of the class that implements IHttpInterface and calls the method ExecuteAsync on this instance and selecting the correct action to execute is the job of the ApiController.ExecuteAsync method, that binds the parameters.

An IActionResultConverter converts the result of the action to an instance of HttpResponseMessage. The response message goes up to the client following the same path as the request.


In this article we took a quick look at the main components that compose the ASP.NET Web API and the role of these components.

Up Next
    Ebook Download
    View all
    View all