A message handler is a class that receives and returns HTTP requests. With message handlers, you can edit and alter any request before it is passed on to HttpControllerDispatcher. Since they are the first and suitable to work with cross-cutting concerns, they are executed earlier.
They are a chain of classes that process and respond through a pipeline. The most important of message handlers is that they will allow you to perform a specific task. This is done within each handler.
There are two types of message handlers in ASP.NET Web API Framework. These are:
• Server-Side HTTP Message Handlers
• Client-Side HTTP Message Handlers
In this article, we will be exploring more about server-side instead of client-side.
Server-side HTTP Message Handlers
On this side of message handlers, you will find built-in message handlers that are used by the Web API pipeline. As the name indicates they are found on the server-side. These handlers namely are:
• HttpServer: it receives a request from the host.
• HttpRoutingDispatcher: it will dispatch the request it received depending on the course of the route.
• HttpControllerDispatcher: this will send the received request to a Web API controller.
There are options in which you can add a custom handler. The more the merrier when it comes to dealing with crosscutting concerns with the HTTP request. They will work better and more efficiently than controller actions.
The benefits of server-side message handlers are:
• Reading and modifying: it allows you to read messages from the request headers. They then will modify it and send it the right way.
• Add a response header: there are times when these handlers can add a response header. This is done to the responses.
• Validate requests: another benefit of server-side message handlers is validating requests. This is usually done before the requests reach the controller.