Interfaces and Classes Used to Create HTTP Handlers in ASP.NET

The .NET Framework provides classes that enable you to handle HTTP requests for the ASP.NET Web pages and services. You can handle HTTP requests by creating a class that implements the IHttpHandler interface contained in the System.Web namespace.

The System.Web namespace contains classes and interfaces that enable you to handle the communication between browsers and Web servers. Before you can use a class that implements the IHttpHandler interface, you need to write the section in the Web.config configuration file to map the class that implements IHttpHandler to a URL request.

Before you create an HTTP Handler, let us look at the IHttpHandler and IHttpHandlerFactory interfaces, and some of the classes contained in the System.Web namespace.

IHttpHandler interface

The IHttpHandler interface must be implemented to create user-defined HTTP handlers to process Web requests. Specific instances of the classes that implement the IHttpHandler interface process the Web requests received by ASP.NET. When you create a class that implements the IHttpHandler interface, you need to implement a method and a property of this interface. The method that needs to be implemented is ProcessRequest, and the property that needs to be implemented is IsReusable.

ProcessRequest

The ProcessRequest method is called whenever an HTTP request is made and has the following Visual Basic .NET syntax:

Sub ProcessRequest (ByVal context As HttpContext)

End Sub

As you can see in the preceding syntax, the ProcessRequest method takes an object of the HttpContext class (discussed later in this section) as a parameter. You use the HttpContext object to handle all Web requests.

IsReusable

The IsReusable property is an overrideable read-only property that gets a value indicating whether the instance of the class that implements the IHttpHandler interface can be recycled and used for other Web requests. The Visual Basic .NET syntax of the IsReusable property is given as follows:

ReadOnly Property IsReusable As Boolean

As you can see in this syntax, the IsReusable property gets a Boolean value. If it gets True, the IHttpHandler instance can be reused for other Web requests. However, if the property gets False, the IHttpHandler instance cannot be reused for other Web requests.

IHttpHandlerFactory interface

As mentioned earlier, the Web requests received by ASP.NET are processed by specific IHttpHandler instances. At run time, the Web requests must be resolved to the IHttpHandler instances. This resolution of the Web requests to the IHttpHandler instances is done by the IHttpHandlerFactory interface. This interface contains two methods, GetHandler and ReleaseHandler.

GetHandler

The GetHandler method returns an IHttpHandler object that processes the Web request from the client. The Visual Basic syntax for the GetHandler method is given as follows:

Function GetHandler( ByVal context As HttpContext, ByVal requesttype As String, ByVal url As String, ByVal pathtranslated As String ) As IHttpHandler End Function The return type of the GetHandler method is IHttpHandler. The different parameters include:

context: Represents the object of the HttpContext class that provides reference to built-in server objects

requesttype: Represents a string value that refers to the method used for HTTP data transfer, such as Get and Post

url: Represents a string value that refers to the URL that is requested by the client

pathtranslated: Represents the string value that refers to the physical path of the application's root directory

ReleaseHandler

The ReleaseHandler method allows releasing an IHttpHandler instance so that it can be reused. The Visual Basic syntax for the ReleaseHandler method is given as follows.

Sub ReleaseHandler( ByVal handler As IHttpHandler)

End Sub

In this code, handler is the IHttpHandler instance that needs to be released.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


No comments:

Post a Comment