HTTP Handlers in ASP.NET - 1

Thursday, March 12, 2009

How to build HTTP Handlers in ASP.NET

The World Wide Web (WWW) uses the Hypertext Transfer Protocol (HTTP) as the
underlying protocol for communication. It is an application-level protocol that is
responsible for establishing a connection between a client (browser) and a Web server
and transmitting the information requested by the client. In fact, the day-to-day life of a
Web server involves receiving requests from clients and responding to them by using
HTTP.
ASP.NET works by dispatching the client requests to user-defined HTTP handler objects
called HTTP handlers. With ASP.NET, you can create these user-defined HTTP
handlers by implementing a .NET interface named IHttpHandler. After you've created a
user-defined handler, you can bind a specific URL request to the handler for handling
specific requests. For example, you can bind a URL request for a file, with your name as
an extension, to a user-defined handler for processing. However, if a specific URL
request is not mapped to a handler, the default handler of ASP.NET handles it.
In this chapter, you will learn about HTTP runtime provided in ASP.NET, which allows
you to process HTTP requests coming from clients. You will also learn about the
interfaces and classes involved in creating HTTP handlers. Finally, you will learn to
create a custom HTTP handler.

Introduction to HTTP Runtime and HTTP Handlers

When you enter a URL in a browser, the browser builds an HTTP request and sends it to
the address specified in the URL. While building the HTTP request, various methods are
used. These methods indicate the purpose of the request. These methods include the
following:
§ Get: Used when a request for a particular page is made. When a user enters
a link in the Address box of the browser or clicks a hyperlink, the HTTP Get
method is used to build the HTTP request. The Get method is usually used
when the request does not alter the state of a database.
§ Head: Used when a user wants to retrieve only the information about the
document and not the document itself.
§ Post: Used when a user requests a resource that interacts with a database.
The Web server, which contains the requested page, performs necessary processing
based on the method used for sending the request, and returns the page requested by
the client. In addition to these methods, you can have a lower-level control over the
processing of requests on the Web server. This is possible with the help of application
programming interfaces (APIs), which are covered in the next two sections.
ISAPI and HTTP Runtime
A number of APIs have been developed that enable developers to have lower-level
control over the processing of requests on the Web server. For example, the Internet
Services API (ISAPI) developed for IIS Web Server enables developers to create highperformance
applications. At the same time, it enables developers to have low-level
control over the way requests are processed by IIS.
With ISAPI, you can create your own dynamic link libraries (DLLs) that specify the tasks
that need to be performed when a request is sent to the Web server. The DLLs provided
in ISAPI can be of two types, filters and extensions. Filters enable you to write code that
can receive notifications from the Web server during the processing of a request. Thus,
filters are used to alter the standard behavior of the Web server. You can use filters to
perform tasks such as compressing and encrypting the data to be sent and
authenticating a user. On the other hand, ISAPI extensions accept user requests,
perform tasks such as retrieving data from a database and generating an HTML page,
and send a response to the client.
In ASP.NET Web applications, low-level control over client requests is achieved by using
the HTTP runtime. The HTTP runtime is built on the Common Language Runtime (CLR)
of the .NET Framework, and provides an environment for processing requests. Thus, the
CLR replaces the ISAPI under IIS. The HTTP runtime performs various functions,
including receiving requests from the client, resolving the address specified in the URL,
and sending the request to the appropriate application for further processing of the
request. The HTTP runtime is capable of receiving multiple requests simultaneously. The
applications are run in separate address spaces, thereby improving reliability and
preventing cross-platform chaos. Therefore, the failure of a single Web application does
not affect the working of the HTTP runtime.
Just like the ISAPI extensions and ISAPI filters, the HTTP runtime enables developers to
have lower-level control over processing Web requests. However, unlike ISAPI, for which
developers must know C++, the HTTP runtime is a cleaner model and enables
developers to program in any .NET programming language. Therefore, ASP.NET prefers
the CLR of the .NET Framework to the ISAPI architecture.


Architecture of the HTTP Runtime


The architecture of the HTTP runtime is similar to that of a pipeline. It is comprised of anumber of HTTP modules and handlers. In simple terms, HTTP modules and HTTPhandlers are classes created by developers that implement predefined interfaces ofASP.NET. When a client makes a request that results in executing a Web application,the request passes through a pipeline of HTTP modules. HTTP modules enable a Webapplication to perform specific tasks, such as encrypting the data, performing customauthentication for providing access to the application, and managing the state of theclient session and the application. After passing through a series of HTTP modules, therequest is sent to the HTTP handler. An HTTP handler is a replacement for ISAPIextensions that receive the request, fetch the required data, and send the data inresponse to the request sent by the client. ASP.NET provides higher-level programmingmodels, such as Web services and Web Forms, which are implemented as HTTPhandlers. The pipeline architecture of the HTTP runtime

Figure 1: Architecture of the HTTP runtime provided in ASP.NET

enables you to easily implementnew functionality by adding new HTTP modules and handlers. Figure 1 depicts thepipeline architecture of the HTTP runtime provided in ASP.NET

You learned about the HTTP runtime, HTTP modules, and HTTP handlers provided in
ASP.NET. Now, you will learn to create an HTTP handler.

We will see Interfaces and Classes Used to Create HTTP Handlers in our Next post

0 comments:

Back to TOP