Difference between HttpHandler and HttpModule.

1. An HttpHandler is generally associated with a specific extension, and is used for things like RSS feeds, dynamic image generation or modification, etc.

An HTTP module is an assembly that is called on every request regardless of extension, and is generally used for things like security, statistics, logging, etc.

2. In the ASP.NET request pipeline HttpHandler comes after HTTP module.

3. HttpHandler actually processes the request and produces response. HTTP module can work on request before and on the response after HttpHandler.

3. HttpHandler implements method ProcessRequest. This method is called when processing the ASP.NET requests. You can perform all the things related to processing request. In HTTP module you implement InIt method that is used for implementing events of HTTP Modules in HTTPApplication.

4.HttpHandler example:
You need to implement IHttpHandler interface

public class SyncHandler : IHttpHandler

and then implement the IsReusable property and the ProcessRequest method of the IHttpHandler interface. Because this is a synchronous handler, return False for the IsReusable property so that the handler is not pooled.

public bool IsReusable
{
   get {return false;}
}

public void ProcessRequest(HttpContext context)
{
   context.Response.Write("Hello from custom handler.");
}

finaly, configure the Web.config

<configuration>
   <system.web>
      <httpHandlers>
         <add verb="*" path="*.sync" type="MyHandler.SyncHandler, MyHandler" />
      </httpHandlers>
   </system.web>
</configuration>

5.HTTP module example:

using System;
using System.Web;
public class HelloWorldModule : IHttpModule
{
    public HelloWorldModule()
    {
    }

    public String ModuleName
    {
        get { return "HelloWorldModule"; }
    }

    // In the Init function, register for HttpApplication
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest +=
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest +=
            (new EventHandler(this.Application_EndRequest));
    }

    private void Application_BeginRequest(Object source,
         EventArgs e)
    {
    // Create HttpApplication and HttpContext objects to access
    // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension =
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx"))
        {
            context.Response.Write("<h1><font color=red>" +
                "HelloWorldModule: Beginning of Request" +
                "</font></h1><hr>");
        }
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension =
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx"))
        {
            context.Response.Write("<hr><h1><font color=red>" +
                "HelloWorldModule: End of Request</font></h1>");
        }
    }

    public void Dispose() { }
}

and in the Web.config file:

<configuration>
  <system.web>
    <httpModules>
      <add name="HelloWorldModule" type="HelloWorldModule"/>
     </httpModules>
  </system.web>
</configuration>
Share this post:Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0Share on Reddit0Email this to someoneDigg this