Handling File Types of Your Own





Handling File Types of Your Own

To write an HttpHandler, you create a class that implements the IHttpHandler interface. All of the handlers in Listing 8.1 do this. You might recall from Chapter 2, "Classes: The Code Behind the Objects," that an interface is used to ensure that a well-known means of communicating with some other code is available. For ASP.NET to communicate with an HttpHandler, it must have a couple members defined by the interface. Listing 8.2 shows the basic interface.

Listing 8.2. A class implementing IHttpHandler

C#

public class MyHttpHandler : IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
    // do something here
  }

  public bool IsReusable
  {
    get { return true; }
  }
}


VB.NET

Public Class MyHttpHandler
  Implements IHttpHandler

  Public Sub ProcessRequest(context As HttpContext) 
    ' do something here
  End Sub

  Public ReadOnly Property IsReusable() As Boolean
    Get
      Return True
    End Get
  End Property
End Class

The ProcessRequest() method is where we do work in response to the request. ASP.NET passes in a reference to the HttpContext object associated with the request. You'll notice in Visual Studio that as soon as you type a period following the context parameter, Intellisense will show you properties that correspond to all the familiar objects you might use in a page (see Figure).

Figure. The properties of the HttpContext object in an HttpHandler.


You can get information about the request via the Request property, and you can send almost any kind of data you want back down the pipe with the Response property. This is very powerful because you can essentially do anything. If you wanted to divert .aspx file requests to a handler of your own (and had way too much time on your hands), you could implement the entire ASP.NET page factory yourself!

The IsReusable property is the other member you'll have to implement in your HttpHandler. It's there to tell ASP.NET whether the instance of the handler can be reused from one request to the next, or if it should create a new instance each time. Generally it's best for this property to return true, especially if the HttpHandler will service many requests. However, if some user-specific action is occurring in the handler that you don't want the next user to use, you should cause the property to return false.

In order for ASP.NET to process any request at all, it must receive the request via IIS. This was covered in Chapter 6. After ASP.NET gets the request, it must designate which HttpHandler will process the request. Listing 8.1 showed the defaults listed in the machine.config file. Listing 8.3 shows the relevant parts of a web.config file to add the HttpHandler to your application.

Listing 8.3. Adding an HttpHandler to your web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="*.jpg" type="MyClass, MyDll" />
    </httpHandlers>
  </system.web>
</configuration>

The element that does the actual work is the <add /> element. The verb attribute indicates the types of requests that are covered (GET or POST), and you can use an asterisk as a wildcard. The path attribute describes the file path of the request. In this example we're using a wildcard with ".jpg" to indicate that we want to handle all requests for .jpg files, but you could just as easily specify anything else, such as "mypage.aspx." The <add /> elements are considered in the order that they appear, so a request that fits the path of two elements will be serviced by the class in the last entry. Finally, the type attribute indicates the name of the class implementing IHttpHandler and the assembly name in which it resides, looking in the /bin folder. Keep in mind that the class name should be fully qualified, meaning that the namespace should be included.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows