Tuesday, December 9, 2008

HttpHandler

Basics

  • An HTTP Handler is a .NET class that executes whenever you make a request for a file at a certain path.
  • The Page class is an HTTP Handler because it implements the IHttpHandler interface.
  • The IsReusable property indicates whether ASP.NET can keep the handler in memory to service multiple requests, or if it must create a new instance of the handler for every request.
  • Unless you maintain some sort of state in the handler, which is uncommon, IsReusable should always return true.
  • Two ways to create an HTTP Handler:
    • Generic Handler (.ashx): you cannot execute a Generic Handler whenever someone requests a file with the extension .gif. If you need more control over when an HTTP Handler executes, create a class implements IHttpHandler.
    • Custom class implement the IHttpHandler interface.

Class implements IHttpHandler

  • Register HTTP Handlers & Configure HTTPHandler Extension in IIS.
    • IIS 6.0
      • In the httpHandlers section of the web.config, add an entry for the file-name extension. Specify the following four attributes:
        • path: specify the path associated with the handler.
        • verb: specify the HTTP verbs, such as GET or POST, associated with the handler. You can specify multiple verbs in a comma-separated list. You can represent any verb with the * wildcard.
        • type: specify the name of the class that implements the handler.
        • validate: specify whether the handler is loaded during application startup. When true, the handler is loaded at startup. When false, the handler is not loaded until a request associated with the handler is made.
      • add the file extension to map to Aspnet_isapi.dll
      • Verify that file exists check box
        • Check. The file-name extension represents a physical file in the application. If the requested file does not exist on disk, IIS displays an error.
        • Uncheck. The file-name extension does not represent a physical file. Instead, the extension is handled dynamically by a class that is mapped to the extension in ASP.NET.
    • IIS 7.0 (Classic or Integrated mode)
      • Using IIS Manager in IIS 7.0 to add a custom handler extension is equivalent to registering the handler extension in the Web.config file of an ASP.NET application. The registration adds a handler element in the handlers section of the system.webServer group.
      • In the httpHandlers section of the web.config, add an entry for the file-name extension.

References:

ASP.NET 2.0 Unleashed

How to: Configure an HTTP Handler Extension in IIS

How to: Register HTTP Handlers

blog comments powered by Disqus