Difference between HttpModule and HttpHandler in .NET
This is quite a popular question in most .NET interviews, and all beginner .NET developers should be able to answer it. In this quick blog post I will try to simply the difference between these two pillars of ASP.NET programming model.
To start with, it is very important to understand that all ASP.NET requests are handled by a web server, a software running on the actualy physical computer at the server side. Internet Information Services, popularly known as IIS is the most common web server shipped with Windows and used to host most ASP.NET applications. This means that all ASP.NET requests by remote clients, like a call to a page http://www.mysite.com/default.aspx, will go through IIS first and then will be passed on by IIS to the ASP.NET runtime (the worker process).
Now before going further, let's discuss about ISAPI first. ISAPI, or Internet Server API is a set of libraries exposed by the IIS in order to allow developers to create custom plugin components for IIS. The ISAPI for IIS has two major components
1. ISAPI Extensions
2. ISAPI Filters
ISAPI Extensions are similar to HttpHandlers in .NET
Let us discuss the ISAPI extensions first. ISAPI extensions are nothing but simple applications created using using Win32 DLLs (a part of the Windows series of Operating Systems, or the Windows API). All "http" requests are passed onto these ISAPI extensions, so when you call: http://www.mysite.com/default.aspx the ISAPI extension created to handle .NET extensions (like there would be an ISAPI extension just to handle ALL .aspx page requests.
What if you want to create your own custom extension in your ASP.NET application? For example, you may want to handle pages like my-page.acv or my-file.vbn extensions. You will have to write your own code in C/C++ using Windows API to create handlers for .acv or any other custom extensions so that these requests can be handled by the IIS server. Unmanaged coding using C/C++ and Windows Library can be quite a pain, thats where HttpHandlers come into the picture. The .NET framework allows you to quickly create your own custom page handlers using HttpHandlers.
HTTP handlers are the .NET components that
implement the System.Web.IHttpHandler interface. Any class that
implements the IHttpHandler interface can act as a target for the
incoming HTTP requests.
ISAPI Filters are similar to HttpModules in .NET
ISAPI filter are again C/C++ code modules that sits between your
Web server and the requesting client. Whenever the remote client makes a web request to the
server, it passes through this filter. You can use HttpModules to create your own custom filters in your ASP.NET application.
If you need to create a request handler, for example you may want your own code to handle all .jpg image file requests like: http://mysite.com/filename.jpg, then you need to use HttpHandlers for this purpose. If you want to modify a certain request, like you may want to perform some custom logic behind the scenes whenever user requests pages like mysite.com/default.aspx, you need to use HttpModules. You can create multiple HttpModules to filter any request.