O'Reilly logo

ASP.NET Cookbook by Geoffrey T. LeBlond, Michael A Kittel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

17.1. Creating a Reusable Image Handler

Problem

You want to create a reusable assembly that retrieves image data from a database and processes it before sending it to a browser.

Solution

Create an HTTP handler to read the image data from the database and send it to the browser.

To implement a custom, reusable HTTP handler:

  1. Create a separate Class Library project in Visual Studio.

  2. Create a class in the project that implements the IHttpHandler interface and place code to handle the request in the ProcessRequest method.

  3. Compile the project as an assembly and place the assembly in the bin directory of your web project.

  4. Add an <httpHandlers> element to the web.config file in your web project referencing your custom HTTP handler.

  5. Reference the URL of the HTTP handler in your application.

Example 17-3 and Example 17-4 show the VB and C# class files we’ve written to implement an image handler as an HTTP handler. Example 17-5 through Example 17-7 show the .aspx file and VB and C# code-behind files for our application that demonstrates the use of the HTTP handler.

Discussion

HTTP handlers are simply classes that implement the IHttpHandler interface. Implementing the IHttpHandler interface requires the implementation of two methods: IsReusable and ProcessRequest. IsReusable is a property that explicitly returns a Boolean value that indicates whether the HTTP handler can be reused by other HTTP requests. For synchronous handlers, like our example, the property should always return false so the handler ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required