Writing and Using HttpModule

You can create and use HttpModule in both existing and new projects. Here I am going to create it in new project.
Open VS2005 and create a class library project. Add a new class called MyModule. Add following code in it.

namespace MyNamespace
{
public class MyModule : IHttpModule
{
public MyModule()
{
}
///
/// Required by the interface IHttpModule
///
public void Dispose()
{
}
///
/// Required by the interface IHttpModule
/// I also wire up the Begin Request event.
///
public void Init(System.Web.HttpApplication Appl)
{
Appl.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
Appl.BeginRequest += new System.EventHandler(OnBeginRequest);
}
public void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
app.Response.Write("Instances of underlying objects have also been created.");
//can do your operations here.
}
public void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
app.Response.Write("Hello World");
//this is the method called on every http request to the server.
}
}
}

Assign strong name to the generated assembly by going in the project properties.
Add assembly in cache by using following command:

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" /i %projPath%\bin\Debug\MyModule.dll /f

Recycle the application pool
iisapp.vbs /a %appPoolName% /r

Add the following line the web application:
<add type="MyNameSpace.MyModule, MyModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3401fe654535df3" name="MyModule">
To pick the public key token you can go to "C:\Windows\Assembly" folder or use "reflector".

Thats all!

Comments

Popular posts from this blog

Unable to delete Shared Services Provider in SharePoint (MOSS)

Cannot add a SimpleContent column to a table containing element columns or nested relations

Future of Hard-Disk Drives?