HttpHandler vs HttpModule – What is the simple difference?

Both HttpHandler and HttpModule are core to the ASP.NET MVC framework. In .NET Core applications, these are replaced by Middlewares

Both HttpHandler and HttpModule are core to the ASP.NET MVC framework. In .NET Core applications, a Middleware does the job of both the HttpHandlers and the HttpModules.

What is a HTTP Module?

HttpModule is a component that executes a specific functionality for every request, such as Security, Authentication, Logging and so on.

The following example illustrates how to do it in an ASP.NET Web Application –

To create a custom HttpModule, you will implement the IHttpModule and then register the component in the web.config.

using System;
using System.Web;

namespace MyWebApplication.Modules
{
    public class MyHttpModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            // Subscribe to the BeginRequest event
            context.BeginRequest += OnBeginRequest;
        }

        public void Dispose()
        {
            // Clean-up code, if needed
        }

        private void OnBeginRequest(object sender, EventArgs e)
        {
            // Intercept and modify the request
            HttpContext context = ((HttpApplication)sender).Context;
            context.Response.Write("<h2>My HttpModule Interception</h2>");
        }
    }
}
<configuration>
  <system.web>
    <httpModules>
      <add name="MyHttpModule" type="MyWebApplication.Modules.MyHttpModule, MyWebApplication.Modules"/>
    </httpModules>
  </system.web>
</configuration>

What is a HttpHandler?

A HttpHandler takes a request and responds with a response. It is generally associated to handle requests related to specific extensions such as RSS, Images, MIME and others. It thus helps in executing requests without having to go till the page level requests (further down the pipeline).

To create a custom HttpHandler, you will implement the IHttpHandler. It is then registered in the web.config as below –

using System.Web;

namespace MyWebApplication.Handlers
{
    public class MyHttpHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // Process the request and provide a response
            context.Response.ContentType = "text/html";
            context.Response.Write("<h2>My HttpHandler Response</h2>");
        }

        public bool IsReusable
        {
            // Set to true if your handler can be reused for multiple requests
            get { return false; }
        }
    }
}
<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="MyHttpHandler.ashx" type="MyWebApplication.Handlers.MyHttpHandler, MyWebApplication.Handlers"/>
    </httpHandlers>
  </system.web>
</configuration>

You can access the created HttpHandler by navigating to a URL that matches the path specified in the Web.config. For example, http://myapplicationdomain/MyHttpHandler.ashx.

Summary

  • Both HttpHandler and HttpModule are core to the ASP.NET MVC framework
  • HttpHandler takes a request and responds with a response
  • HttpModule is a component that executes a specific functionality for every request, such as Security, Authentication, Logging and so on.
  • HttpHandler is associated to handle requests related to specific extensions such as RSS, Images, MIME and others.
  • a HttpModule implements the IHttpModule and HttpHandler implements the IHttpHandler. Both are registered in the web.config

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *