Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
How To Create Your Own Http Module.
The purpose this tutorial is to understand what is a Http module and how to create your own Http module. Through out this tutorial I will be using C# to explain some of the concepts.
Reader is assumed to know how to use of Visual Studio. Has good understanding of ASP.NET web application and last but not least know programming using .NET base class libraries framework.
What is an Http module?
So what is a Http module?
Simply put, Http module is a piece of code that sits between the client and the web server. All the request that are made through the web server passes through http module.
Lets have a look at very high level, how the request are process by ASP.NET framework. When a client makes a request to your web server the request comes in the form of Http Request. The Http Request flows through series of Http Modules. Each of the Http module have full control over the Http request. They process or modify the request. On the request passes through all the modules it is handed over to Http handler. Http handlers process the request and response passes through the all Http modules in reverse order.
ASP.NET framework already has few Http modules such as session state, authorization etc. You can find list of these modules in the machine.config file under httpModules section.
So what we can we do with Http module?
If we are creating our own Http module, we can respond to either ASP.NET events or user events in a way we want it to respond.
Authoring Http Module.
The following steps are followed while creating an Http module.
Learn by example.
It is always easy to understand if it is explained with example. Let us follow the same practice. Let us say I have a requirement where I need to log the requests that are coming to my web application. Later on I will use this information to analyze my web site traffic.
At this stage RequestLoggerModule code should look like this
The best place to log the request is the BeginRequest. In the Init method attach a event handler to BeginRequest event of the HttpApplication as shown below.
context.BeginRequest += new EventHandler(context_BeginRequest);
In the context_BeginRequest method trap the request stream and log it to a file as shown below.
Note that writing to a file will through exception if the user context under which ASP.NET is running do not have write access. It is not a good idea to give a write permission in your real application.
Now your code should look like below.
Configuring web application.
We have built the Http module successfully, let us see how we can make use of the module that we have created.
Follow the below steps to configure your existing ASP.NET we application to use the RequestLoggerModule.
If you want this module to be used by all the application that are hosted on your server, you need to add the above section to machine.config file.
comments powered by Disqus