In this article i will discuss about HttpHandlers in Asp.net. In ASP.NET request processing pipeline model each http requests passes though different modules in the pipeline. Each module receives the http request and has full control over it. The module can play with the request in any way it sees fit. Once the request passes through the entire HTTP modules, it is eventually served by an HTTP handler. The HTTP handler performs some processing on it, and the result again passes through the HTTP modules in the pipeline.
During the processing of an http request, only one HTTP handler will be called, whereas more than one HTTP modules can be called.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. HTTP handlers are somewhat similar to ISAPI extensions. One difference between HTTP handlers and ISAPI extensions is that HTTP handlers can be called directly by using their file name in the URLAn ASP.NET HTTP handler is the process you can think it as end point to a incoming request. 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. We can create our own HTTP handlers that render custom output to the browser.Creating a Custom HTTP HandlerTo create a custom HTTP handler, you create a class that implements the IHttpHandler interface to create a synchronous handler. Alternatively, you can implement IHttpAsyncHandler to create an asynchronous handler. Both handler interfaces require that you implement the IsReusable property and the ProcessRequest method. The IsReusable property specifies whether the IHttpHandlerFactory object (the object that actually calls the appropriate handler) can put the handler in a pool and reuse it to increase performance. If the handler cannot be pooled, the factory must create a new instance of the handler every time that the handler is needed.The ProcessRequest method is responsible for processing individual HTTP requests. In this method, you write the code that produces the output for the handler.HTTP handlers have access to the application context. This includes the requesting user's identity (if known), application state, and session information. When an HTTP handler is requested, ASP.NET calls the ProcessRequest method of the appropriate handler. The code that you write in the handler's ProcessRequest method creates a response, which is sent back to the requesting browser.Handlers enable the ASP.NET framework to process individual HTTP URLs or groups of URL extensions within an application. Unlike modules, only one handler is used to process a request. All handlers implement the IHttpHandler interface, which is located in the System.Web namespace. Handlers are somewhat analogous to Internet Server Application Programming Interface (ISAPI) extensions.Configuring HTTP HandlersThe <httpHandlers> configuration section handler is responsible for mapping incoming URLs to the IHttpHandler or IHttpHandlerFactory class. It can be declared at the computer, site, or application level. Subdirectories inherit these settings.Administrators use the <add> tag directive to configure the <httpHandlers> section. <Add> directives are interpreted and processed in a top-down sequential order. Use the following syntax for the <httpHandler> section handler:<httpHandlers><add verb="[verb list]" path="[path/wildcard]" type="[COM+ Class], [Assembly]" validate="[true/false]" /><remove verb="[verb list]" path="[path/wildcard]" /><clear /></httpHandlers>Practical ExampleI will explain an example to use the handler to return an image. Below is the codeIn ashx file (Handler)
using System.Web; using System.Web.Services; namespace TestProject { /// <summary> /// Summary description for $codebehindclassname$ /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class ImageHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { //Logic to retrieve the image file context.Response.ContentType = "image/bmp"; context.Response.WriteFile("images/Balloon.bmp"); } public bool IsReusable { get { return false; } } } }
Now to test that handler I have created an aspx page
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <img src="ImageHandler.ashx" /> </div> </form> </body> </html>
Above you saw the image is rendered via handler. We can extend the above example by passing querystring parameters to our handlerand using them to perform additional logic in our handler.
Do let me know your feedback,comments.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18