HTTP Handlers and HTTP Modules in ASP.NET
Author: Nikki Smith
In this article, I will explain HTTP Handlers and HTTP Modules I will also write difference between HTTP Handlers and HTTP Modules.
HTTP Handlers and HTTP Modules are an essential part of ASP.NET architecture. Both these concepts are mostly asked in ASP.NET interviews and used in ASP.NET when a pre-processing logic has to be implemented before a request hits the IIS resources. In ASP.NET, a request flows through and processed by multiple HTTP modules. After passing each module the request is handled by a single HTTP handler and then the response of the request flows back through the HTTP modules to the user.
HTTP Handlers is frequently referred as the endpoint. It is actually the process that runs in response to a request made to an ASP.NET web application. It can be considered as a program which runs some code and handles the request when a user sends it. An HTTP Handler basically processes the request and produces the response. An HTTP handler is normally associated with a specific extension and is used for things like RSS feeds, dynamic image generation or modification. All handlers implement the IHttpHandler interface.
HTTP handlers can be either synchronous or asynchronous. A synchronous handler finishes its processing and does not return response until it finishes. On the other hand, an asynchronous handler runs a process independently of sending a response to the user. Asynchronous handlers are very useful when someone needs to start a lengthy process and the user does not need to wait until it finishes before getting a response from the server.
ASP.NET page handler is the most common handler that processes .aspx files. The request is processed by the page via the page handler when users request an .aspx file. There are several built-in ASP.NET HTTP handlers that includes ASP.NET page handler, web service Handler, ASP.NET user control handler and Trace handler.ASP.NET provides the facility to create custom handlers so you can create your own handlers in ASP.NET. You have to create a class that implements IHttpHandler interface to create a custom synchronous HTTP Handler. To create an asynchronous HTTP Handler, you need to create a class that implements IHttpAsyncHandler interface. Typical uses of custom HTTP handlers are RSS feeds and image server.
HTTP module is event based processor. It will execute for every request to your application regardless of extension. An HTTP module is actually an assembly that is called on every request to your application. HTTP modules are fully integrated with the life cycle of an ASP.NET application and called as part of ASP.NET request pipeline. HTTP modules are called before and after the handler executes and you examine incoming and outgoing requests and take action based on the request. They enable developers to intercept and modify requests. HTTP modules implents IHttpModule interface. You can create your own custom HTTP module by creating a class that implements IHttpModule interface and registering into web.config file. HTTP modules are generally used for things like security, custom headers and footers, logging and statistics.
Difference between HTTP Handlers and HTTP Modules
- HTTP handler is an extension based processor while HTTP module is an event base processor.
- Http handler is where the request train is headed. Http Modules are stations along the way.
- Only one handler is used to process a request while multiple modules can be used for a single request.
- Handlers implement the IHttpHandler interface and Modules implement IHttpModule interface.
- HTTP module can work on request before and on the response after HTTP handler.
- HTTP Handlers are the endpoint while modules are called before and after the handlers.