Sunday, January 8, 2012

HTTP Handlers in ASP

HttpApplication is the type that manages the request as it moves through the pipeline. Up to
now we’ve examined the events along that pipeline and the mechanisms at your disposal for
extending its functionality. A critical step of that process is creating and executing the request
handler. The Pagehandler, which is an instance of System.Web.UI.Page(or any type derived
from that type), deals with ASPX pages. In this section we’re going to take a look at what it
takes to be a handler that the Framework recognizes, some of the other handlers that are built
into the Framework, and how to create your own handlers to custom process specialized
requests.
So what does it take to be a handler? How does the Framework know how deal with an
instance of the Pageclass, a derived type that didn’t exist at the time the Framework was
compiled? Via polymorphism, of course.
The only thing the pipeline cares about is a simple interface named IHttpHandler. Any
type that implements this interface qualifies to receive requests from the ASP.NET Framework
and process them however it sees fit. Once a type implements this interface, it’s associated
with requests via any combination of file name, file extension, or request type.
For example, the extension ASPX is mapped to the Page handler factory. The pipeline
hands the request off to this type by calling a method on the IHttpHandlerinterface. This class
looks at the request, creates an instance of the corresponding page object, and hands the request off to it via the same interface method.
Handlers Built into the Framework
A few handlers are built into the ASP.NET 1.x versions of the Framework, and ASP.NET 2.0 adds
quite a few more. Handlers can be used for any type of specialized request processing. They
can be mapped to a specific URL (as is the case with trace.axd), or the can be mapped to a
specific extension (as is the case with *.aspx).
Handlers can also respond to specific HTTP request types (GET, POST, HEAD, and oth-
ers). There actually is a handler that rejects outright any request type that is not a GET, POST,
or HEAD (the HttpMethodNotAllowedhandler).
Table 2-4 is a list of the handlers built into the Framework and a brief description of the
work that they do. A detailed discussion of some of the more prominent handlers follows.

No comments:

Post a Comment