Short Snippet For Image Handler in ASP.NET: Real World Scenario

HTTP Handlers: Yesterday we encountered an actual scenario when we bound an image to an ASP.NET Image Server control directly. We tried various ways to bind however none of them succeeded. than googled up and found some great advice from .Net Geeks.

Eventually I looked up the way to create an HTTP handler. Though that code I found was too tricky that I don’t require. Later I prepared fresh code that fulfills my desire to bind images to an Image Control at runtime. It’s just a few lines of code.

Before proceeding let’s have a look at and an explanation of HTTP Handlers.

HTTP Handlers

An ASP.NET HTTP handler is the process 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.

The ASP.NET page handler is only one type of handler. ASP.NET comes with several other built-in handlers such as the Web service handler for .asmx files.

HTTP handlers have access to the application context, including the requesting user's identity (if known), application state, and session information. When an HTTP handler is requested, ASP.NET calls the ProcessRequest method on the appropriate handler. The handler's ProcessRequest method creates a response, that is sent back to the requesting browser.

HTTP Handlers in ASP.NET

ASP.NET maps HTTP requests to HTTP handlers based on a file name extension. ASP.NET includes several built-in HTTP handlers, as listed in the following table.

Handler Description
ASP.NET Page Handler (.aspx) The default HTTP handler for all ASP.NET pages.
Web service handler (.asmx) The default HTTP handler for Web service pages created using ASP.NET.
ASP.NET user control handler (*.ascx) The default HTTP handler for all ASP.NET user control pages.
Trace handler (trace.axd) A handler that displays current page trace information. For details, see How to: View ASP.NET Trace Information with the Trace Viewer.

Custom HTTP Handler

Kindly have a look at the Handler class, that I have created, shown in the image below:

Handler

Each handler that implements IHttpHandler must provide the implementation of its method and property.

public bool IsReusable
{
    get
    {
       return false;
   }
}
public void ProcessRequest(HttpContext context)
{
}

In my example, I’ve bound a Handler to an Image Server Control to bind the image.

Bind Handler

Whenever the page is loaded, it calls the Handler to process the request and binds the images to the Image Control that was not possible earlier. This code is a very short code snippet. 


Similar Articles