Http Handler vs Http Module

What is ASP .Net  HTTP Handler:

      An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser

Reference: http://msdn.microsoft.com/en-us/library/bb398986.aspx

What is ASP.Net HTTP Module:

      An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.

Reference: http://msdn.microsoft.com/en-us/library/bb398986.aspx

Note:The basic different between HTTP Handler and HTTP Module, is that HTTP Module is event based processor.

Example of How to implement HTTP Handler:

using System;
using System.Web;
using System.IO;
namespace MyNamespace
{
public class MyHttpHandler  : IHttpHandler
{
    public void ProcessRequest(System.Web.HttpContext context)
    {
           context.Response.Write("The page request is " + context.Request.RawUrl.ToString());
           StreamWriter sw = new StreamWriter(@"C:\requestLog.txt",true);
           try
           {
                sw.WriteLine("Page requested at " + DateTime.Now.ToString() +
                              context.Request.RawUrl); sw.Close();
           }
           finally {  sw.Close();    }
    }
    public bool IsReusable
     {
            get
            {
                    return true;
                }
        }
}

 

Before you will be able to do Call for this HTTP Handler, you need to do the followings:
      1.  Add the following tags into your Web.Config file

<system.web>
    <httpHandlers>
          <add verb="*" path="*.WhatEverExtension" type="MyNamespace.MyHttpHandler  , MyAssemblyName"/>
      </httpHandlers>
</system.web>

2.  You need to add the extension you added on step 1 to your IIS so that IIS will recognize the new extensions… on our example you need to add "WhatEverExtension" to IIS
Example of how to implement HTTP Module:

namespace MyNamespace
{
public class MyHttpModule : IHttpModule
{
public MyHttpModule()
{}
public void Init(HttpApplication objApplication)
{
objApplication.BeginRequest += new EventHandler(context_BeginRequest);
objApplication.EndRequest += new EventHandler(context_EndRequest);
}
public void Dispose()
{
}
public void context_EndRequest(object sender, EventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"C:\requestLog.txt",true);
sw.WriteLine("End Request called at " + DateTime.Now.ToString());
}
finally {sw.Close(); }
}
public void context_BeginRequest(object sender, EventArgs e)
{
try
{
StreamWriter sw = new StreamWriter(@"C:\requestLog.txt",true);
sw.WriteLine("Begin request called at " + DateTime.Now.ToString());
}
finally { sw.Close(); }
}
}
}

For HTTP Module you need also to register this HTTP Module on your Web.config file… to do so , add the following tags to your Web.config file :

<httpModules>
        <add name="clsMyModule" type="MyNamespace.MyHttpModule, MyAssemblyName"/>
</httpModules>

List of HTTP Module Supported Events by :

 

Event Name Description
AcquireRequestState This event is raised when ASP.NET runtime is ready to acquire the Session state of the current HTTP request.
AuthenticateRequest This event is raised when ASP.NET runtime is ready to authenticate the identity of the user.
AuthorizeRequest This event is raised when ASP.NET runtime is ready to authorize the user for the resources user is trying to access.
BeginRequest This event is raised when ASP.NET runtime receives a new HTTP request.
Disposed This event is raised when ASP.NET completes the processing of HTTP request.
EndRequest This event is raised just before sending the response content to the client.
Error This event is raised when an unhandled exception occurs during the processing of HTTP request.
PostRequestHandlerExecute This event is raised just after HTTP handler finishes execution.
PreRequestHandlerExecute This event is raised just before ASP.NET begins executing a handler for the HTTP request. After this event, ASP.NET will forward the request to the appropriate HTTP handler.
PreSendRequestContent This event is raised just before ASP.NET sends the response contents to the client. This event allows us to change the contents before it gets delivered to the client. We can use this event to add the contents, which are common in all pages, to the page output. For example, a common menu, header or footer.
PreSendRequestHeaders This event is raised just before ASP.NET sends the HTTP response headers to the client. This event allows us to change the headers before they get delivered to the client. We can use this event to add cookies and custom data into headers.
ReleaseRequestState This event is raised after ASP.NET finishes executing all request handlers.
ResolveRequestCache This event is raised to determine whether the request can be fulfilled by returning the contents from the Output Cache. This depends on how the Output Caching has been setup for your web application.
UpdateRequestCache This event is raised when ASP.NET has completed processing the current HTTP request and the output contents are ready to be added to the Output Cache. This depends on how the Output Caching has been setup for your Web application.