This article series is explaining HttpHandler and HttpModule. The purpose of this article is to understand the use of HttpHandler and HttpModule in real time software development. In our previous few articles, we learned various concepts of HttpHandler. You can read them here.
- HttpHandler and HttpModule in Real Scenario: Getting Started with HttpHandler
- HttpHandler and HttpModule in Real Scenario: Few examples of HttpHandler-1
- Working with Image in HttpHandler
- Working With Header in HttpHandler
From this article we will start to talk about HttpModule that is another interesting concept of the HTTP request and response pipeline process. As was explained earlier in this series something is not implemented for theoretical understanding, in this article we will understand the basic implementation of HttpModule and in future articles we will see a few realtime implementations with HttpModule. So, let’s start with some theory.
What HttpModule is
In the web and MSDN you will find a much better definition of HttpModule then my definition. Just for the sake an introduction I will just write a few lines about HttpModule. An HTTP module is an assembly that is called on every request to an application. It’s a part of the HTTP request and response pipeline.
Even the ASP.NET engine uses HttpModule to perform certain operations (which are outside our interests here). We are interested in implementing our own HttpModule that we will inject into a HTTP request and response pipeline.
Since the HttpModule executes in each and every request, we can implement such logic/checking that needs to be performed for each and every HTTP request or response. For example.
- To check request type
- To perform security checking
- To log incoming requests from a different location for business needs
And many more that we will be implementing in future articles.
Ok, we have learned the basic concept of HttpModule. If not please consider any good reference of HttpModule for understanding the primary concept. We will now implement HttpModule in an ASP.NET application.
Implement simple HttpModule in ASP.NET application
This is the first example in the HttpModule series. Here we will learn to add a HttpModule to our ASP.NET application. So, create one empty Web application and add one .ashx file.
Once you add the file, just copy and paste the following code into that .ashx file.
As we said earlier, HttpModule is nothing but a chunk of code that we will inject into the HTTP request and response pipeline.
Here the chunk of code is nothing but a class. To implement the custom HttpModule we need to inherit our class from the IHttpModule interface That contains the Dispose() and Init() methods.
In the following example we have Implemented IHttpModule in the MyHttpModule class. The Init() method will execute first for any HTTP request.
So, this is our small MyHttpModule class implementation that is doing nothing in fact. We have just learned how to inject a HttpModule into a HTTP request and response pipeline process.
- namespace WebApp
- {
- public class MyHttpModule : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- throw new NotImplementedException();
- }
- }
- }



Lalit RaghavPosted Sep 3, 2017, 8:17 AM
Nice Article for beginner