HTTPHandler is a low level request and response API in ASP.Net for injecting pre-processing logic to the pipeline based on file extensions and verbs An HTTPhandler may be defined as an end point that is executed in response to a request and is used to handle specific requests based on extensions. The ASP.Net runtime engine selects the appropriate handler to serve an incoming request based on the file extension of the request URL. On the contrary, an HttpModule is a component that is part of the ASP.Net request processing pipeline and is called on every request that is made to your application. Note that the basic objective of both HTTPhandlers and HttpModules is to inject pre-processing logic to the pipeline. Assume that your application needs to serve images of varying sizes – you can take advantage of a custom HTTPhandler to resize those images and send the response back. Another scenario where you might want to use a custom HTTPhandler is when you would like to execute some pre-processing logic in your application based on the extensions. Although you can do almost anything you can do with HTTPhandler also with your ASP.Net page, HTTPhandlers are much more portable and reusable than your web pages. When a request for a resource comes to the ASP.Net Engine, the ASP.Net Worker Process in turn instantiates the appropriate HTTPhandler to server the request based on the extension. An HTTPhandler in ASP.Net is a class that implements the IHTTPhandler interface. Incidentally, the IHTTPhandler interface is available in the System.Web namespace. Note that the PageHandlerFactory implements the IHTTPhandlerFactory interface and contains a method called GetHandler which in turn is responsible for returning the appropriate handler to server the particular request. The MSDN states: “An ASP.Net HTTPhandler 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 via the page handler.” Creating a custom HTTPhandler In this section we will explore on how we can build a custom HTTPhandler in ASP.Net. To build a custom HTTPhandler, create a class that implements the IHTTPhandler as shown in the code snippet below. namespace CustomHTTPhandler { public class IDGCustomHTTPhandler : IHTTPhandler { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { throw new NotImplementedException(); } } } Note that your custom HTTP handler should have a property called IsReusable and a method called ProcessRequest. While the former is used to specify whether the handler can be reused, the latter is a method that does the actual processing for you. In essence, any custom HTTPHandler should implement the IHttphandler interface and define these two members. Registering your handler The mapping information for the HTTPhandlers are available in the configuration files. Here’s how a section of your machine.config file might look like. <add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" /> <add verb="*" path="*.config" type="System.Web.HttpForbiddenHandler" /> <add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory" /> Now, you would also need to let the runtime know when your custom HTTPhandler should be invoked. Where should you specify this? Well can specify such details in the web.config file. You can add and remove HTTPhandlers using the configuration section in your applications configuration file. Here’s how you can register your handler in the application’s web.config file. <httpHandlers> <add verb="*" path="*.idgaspx" type="IDGCustomHTTPhandler"/> </httpHandlers> So, what did we do here? We just registered our handler and specified that if any request for an extension of .idgaspx arrives, such requested should be routed to the custom Http Handler named IDGCustomHTTPhandler. Note that unlike .aspx web pages, the HTTPhandlers don’t have visual elements. You can create your HTTPhandlers in a custom library and then reuse them as and when they are needed. Asynchronous HTTPHandlers The newer versions of ASP.Net provide support for asynchronous Http handlers. You can take advantage of async/await and the TPL to build asynchronous HTTPhandlers in ASP.Net. To create a custom asynchronous HTTPhandler, you should inherit the HttpTaskAsyncHandler class. The HttpTaskAsyncHandler abstract class in turn implements the IHttpAsyncHandler and IHTTPhandler interfaces. The following code snippet illustrates how our custom asynchronous HTTPhandler looks like at first glance. public class IDGCustomHTTPhandler : HttpTaskAsyncHandler { public override Task ProcessRequestAsync(HttpContext context) { throw new NotImplementedException(); } } Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos