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

Read more...

Why Should I Use ASP.NET 3.5?

Sunday, March 8, 2009

Why Should I Use ASP.NET 3.5?

It seems like everytime I turn around a new version of some technology is coming out, or some new way of doing the same old thing is in the works and therefore demands my attention. It's easy to feel overwhelmed with the never ending onslaught of new technologies. Fortunately, ASP.NET 3.5 is a rather minor upgrade from ASP.NET 2.0; that is, there are not that many new ASP.NET-specific features in the .NET Framework 3.5.

There are three new features worth noting in ASP.NET 3.5:
Integrated ASP.NET AJAX support,
The ListView control, and
The DataPager control Prior to ASP.NET 3.5, Microsoft's ASP.NET AJAX framework needed to be downloaded and installed as an extension. With ASP.NET 3.5, however, the AJAX-related classes are built directly into the .NET Framework, making it easier to get started building rich, AJAX-enabled Web applications with ASP.NET. (For more information on using the ASP.NET AJAX framework, check out my article series, Building Interactive User Interfaces with Microsoft ASP.NET AJAX.) ASP.NET 2.0 introduced a number of new data Web controls, including the GridView, DetailsView, and FormView controls. The GridView displays a list of records in a boxy, grid-like table. The DetailsView and FormView each display one record at a time, the DetailsView in a grid-like output and the FormView in a more fluid layout using templates. The ListView control is an update to the old DataList and Repeater controls, displaying mulitple records and providing functionality like the GridView, but allowing for a more flexible layout through the use of templates. The DataPager control operates as a sort of free-standing paging interface. In short, it renders a paging user interface - next, previous, first, last buttons, for instance - and is tied to a data Web control. The DataPager only works with those controls that implement the IPageableItemContainer interface, which (currently) includes only the ListView control.

For a step-by-step demo using the ListView and DataPager controls, check out Scott Guthrie's blog entry, The Control. Microsoft has also published videos showing how to use the ListView and DataPager controls.

An Overview of Framework Features Useful to ASP.NET DevelopersThe main framework feature in the .NET Framework version 3.5 is LINQ, which stands for Language Integrated Query. LINQ makes SQL-like syntax a first-class citizen in VB and C#, allowing developers to work with data using code that looks like:

// C# example...
List productNames = from p in products where p.CategoryID = 1
select p.ProductName;

LINQ simply defines a syntax for working with data. There are built-in Object Relational Mappers (ORMs) that will create the underlying framework to generate LINQ statements that operate against a database, XML, or an object layer. Scott Guthrie has a great multi-part tutorial series on using the LINQ to SQL ORM implementation.
Visual Studio 2008 provides full-IntelliSense support when using the LINQ syntax. The LINQ syntax is possible due to a number of new language features in C# and Visual Basic, including automatic properties, extension methods, and lambda expressions

Download The required Material for Programming Asp.NET 3.5

We will discuss more on further post
Stay tuned to Expert's Point

Read more...

Back to TOP