Monday, April 14, 2014

HTTP Handlers: Last day we encountered a real world scenario when we stuck to bind and image to Asp.Net Image Server control directly. We tried different ways to bind, though it didn’t succeed .than googled up and found some great advice from .Net Geeks.

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

Before to move ahead let’s have a look and conversant about 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, which 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 on the Handler class, which I have created in, depict image below:

clip_image001

Each handler which implements IHttpHandler has to give the implementation of its method and property.

public bool IsReusable

{ get {return false;} }

public void ProcessRequest(HttpContext context)

{ }

In my example I’ve bind Handler to Image server control to bind the image.

clip_image003

Whenever page gets loaded, it calls the Handler to process the request and bind the images to Image control, which was not possible earlier. This code is having very short code snippet.

Hope it would help you Smile

Keep Smiling.

0 comments :

Post a Comment