20.1. Creating a Reusable Image Handler
You want to create a reusable assembly that retrieves image data from a database and processes it before sending it to a browser.
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:
Create a separate Class Library project in Visual Studio.
Create a class in the project that implements the
IHttpHandlerinterface and then place code to handle the request in the
Compile the project as an assembly and place the assembly in the bin directory of your web project.
<httpHandlers>element to the web.config file in your web project referencing your custom HTTP handler.
Reference the URL of the HTTP handler in your application.
Examples 20-3 and 20-4 show the VB and C# class files we’ve written to implement an image handler as an HTTP handler. Examples 20-5, 20-6 through 20-7 show the .aspx file and VB and C# code-behind files for our application that demonstrates the use of the HTTP handler.
HTTP handlers are classes that implement the
IHttpHandler interface. Implementing the
IHttpHandler interface requires the implementation of two methods:
IsReusable is a property that explicitly returns a Boolean value that indicates if 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 is not pooled (kept ...