.NET has two main musketeers which are known as HttpModule and HttpHandler. Both have a vital role and work as per their need. Web API also uses HttpHandler to handle the HttpRequest. In very basic words Handler is “An agent who handles something”. HttpHandler works in the same way and takes care of Http requests which is also a hub ofthe Http universe. A simple example can be, “Client sends a request to Http server than server accepts this and takes anticipated processing and send back to client as HttpResponse."
In this article, we’ll see how Http Message handler works in Web API and actual flow of each Http request. Here is a diagram which shows the execution flow of each request and processing of each handler exists.

Note: This diagram is as per my understanding of Web API pipeline.
As you can see from the image shown above, Web API is hosted somewhere on IIS of self-hosted. An HttpRequest comes to HttpServer and converts into HttpReuqestMessage, which is further routed to Delegate Handler. Specifically there are two forms of message handler pipelines in Web API. First is normal handler pipeline in which all requests for all routes pass through and second one relates to specific route and called as per route. In general all requests come to normal handler pipeline and areprocessed, whilst per route comes in picture only when there is a specific route (May be any Http request should not processes or that route should be ignored).There can be plenty of scenarios.
Here, I’ve a structure of the solution that has two handlers as in the following screenshot,
A MessageHandler is normal class file which inherits an abstract classDelegatingHandler. Two handlers exist in solution CustomHanlder_one and CustomHanlder_Two.
A code segment for two handlers named as CustomHandler_one and CustomHandler_Two are shown below:
- namespace WebApiDemo
- {
- public class CustomHandler_One: DelegatingHandler
- {
- protected async overrideTask < HttpResponseMessage > SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- // Call the inner handler.
- var response = awaitbase.SendAsync(request, cancellationToken);
- response.Headers.Add("X-Powered-By", "DotnetPiper : Value returned from CustomHandler One.");
- Debug.WriteLine("Process response");
- return response;
- }
- }
- }
- CustomHandler_Two.cs code segment as given below:
- namespace WebApiDemo
- {
- publicclassCustomHandler_Two: DelegatingHandler
- {
- protectedoverrideTask < HttpResponseMessage > SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- // Create the response.
- var response = newHttpResponseMessage(HttpStatusCode.OK)
- {
- Content = newStringContent("I'm called from Custom Delegate Hanlder Two !")
- };
- // Note: TaskCompletionSource creates a task that does not contain a delegate.
- var tsc = newTaskCompletionSource < HttpResponseMessage > ();
- tsc.SetResult(response); // Also sets the task state to "RanToCompletion"
- return tsc.Task;
- }
- }
- }

Let’s see execution order of global handler and how can we validate and control the response.
Here, I’ve a simple controller Employees which has the following Action Methods defined below and performs a set of statement like fetch a record from collection. GetDetails() action method takes one parameter and has its own specific routes while GetEmployeeByID() action method takes two parameters as arguments and has its own defined route.
GetDetails
- public Employee GetDetails(int id)
- {
- return listEmp.First(e => e.ID == id);
- }












Pradeep SahooPosted May 15, 2016, 11:11 PM
Nice information .Thanks for sharing.
Humayun Kabir MamunPosted Apr 11, 2016, 3:49 AM
Nice...
K P Singh ChundawatPosted Apr 9, 2016, 2:13 AM
Knowledgeable .Thank for sharing .
Debasis SahaPosted Apr 9, 2016, 2:05 AM
Good share..
Sachin KaliaPosted Apr 8, 2016, 10:46 PM
Thanks Guys...
Vignesh ManiPosted Apr 8, 2016, 5:18 PM
Nice one
Mohammed IbrahimPosted Apr 8, 2016, 2:27 PM
nice
Rahul Kumar SaxenaPosted Apr 8, 2016, 7:37 AM
Good One..