The last story tells httpmodule, keeps persistent efforts to find httphandler
Author: SUN Ya-min This article taken from: CCID December 26, 2002
Basic model of HTTP treatment pipeline
To study HttpModule and IHTTPHANDLER, you must first have a understanding of the ASP.NET processing pipeline.
In the ASP.NET application, the system uses a set of associated classes to handle the client's request (Request), and the processing mode of the ASP.NET application can be used as an HTTP processing pipe. HttpModule and Ihttphandler are two processing links on this processing pipeline.
The class in the HTTP processing pipe is defined in the System.Web namespace, mainly with the following types:
· The HTTPWorkerRequest abstraction class defines the basic method of the ASP.NET page processing request;
· HTTPRuntime provides a set of services for processing applications;
· HTTPContext saves all related context information that handles a request;
· HTTPApplicationFactory provides applications for related catalogs;
· Httpapplication Defines the general method, attributes, and events of all ASP.NET applications. This class is also based on the base class defined in the user in the global.asax file;
· Modules process before and after the response;
· HandlerFactories offers the handlers in your application;
· Handlers handle requests and responses.
The model of the HTTP treatment pipeline is as follows:
Figure 1: HTTP treatment pipeline
On the Windows platform, HTTP Pipline requires IIS support. In order to run the ASP.NET application, IIS requires the following two files: aspnet_isapi.dll and ASPNET_WP.EXE
· ASPNET_ISAPI.DLL is an ISAPI EXTENTION to transfer ASPNET_WP.EXE processing
· ASPNET_WP.EXE uses HTTPRuntime to process specific processing
The process of processing can be represented by the figure as follows:
Figure 2: HTTP treatment pipe on IIS
HTTPHANDLER
Httphandler implements a function similar to the ISAPI EXTENTION, which handles request (response). The implementation of the HTTPHANDLER function is achieved by implementing the IHTTPHANDLER interface. In fact, when we write the ASP.NET page, the base class inherited by the ASP.NET page - System.Web.ui.page-- also implements the HTTPHANDLER interface, also a httphandler, look at its definition, know it. (C #):
Public Class Page: TemplateControl, IHTTPHANDLER
The definition of the interface IHTTPHANDLER is as follows:
Interface IHTTPHANDLER
{
Void ProcessRequest (httpContext CTX);
Bool isreuseable {get;}
}
ProcessRequest is a place to add your own code in the interface. The isreUseable property indicates whether the implementation class of the HTTPHandler needs to be cached.
The following example shows the basic use of HTTPHANDLER:
1. Create a project called MyNameSpace, add a class, name is MyHandler, the code is as follows:
example 1:
Namespace mynamespace
{
Public class myhandler: httphandler {
Public Void ProcessRequest (HttpContext CTX)
{
HTTPRESPONSE RESPONSE
Response.write ("this is my handler");}
Public Bool IsReusable
{
Get {return true;}
}
}
}
2. Compile the above code to generate myNamespace.dll files;
3. Create a new WebApplication project, or open a WebApplication project, add file MyNameSpace.dll to the project's reference, or copy it to the bin directory of the project;
4, modify web.config, add the following:
TYPE = "MyNamespace.myHandr, MyNamespace" /> httphndlers> configure> Options in the configuration file Description: · VERB can be "get" or "post", indicating a request for GET or POST. "*" Means processing all requests. · PATH indicates that the corresponding file is processed, "*. ASPX" means processing the request sent to all ASPX pages. You can specify the path, such as "/test/*.aspx", indicating that only the ASPX files under the TEST directory are processed. In the Type property, the string before comma indicates the class name of the HTTPHandler's implementation class, and the back string indicates the name of the DLL file. Now, request any ASPX page in the project, the page is displayed on the page is always as follows: This is my handler Because our customized handler intercepts all requests to the ASPX page, and use their own methods to handle these requests. In order to make our ASPX page can run smoothly, we need to modify the web.config file: TYPE = "MyNamespace.myHandr, Hander" /> httphndlers> configure> In order to let the files named .foo can be run by our Handler intercepted, we need some additional work. Open the IIS management console, click on the site, select Properties, and jump out of the property dialog. Select the primary directory option. Figure 3: Figure 3: Web Site Properties dialog Select the configuration, pop up the application configuration dialog, add ".foo" to the application map, as shown in Figure 4: Figure 4: Adding an application mapping Ok, we can now add a .foo file in the project, when sending a request to the file, the browser displays: This is my handler has no impact on other ASPX files. Implement Handler Factory Another option to implement the HTTPHANDLER function is to implement a Handler Factory, which is implemented by implementing the IHTTPHANDLERFACTORY interface. The IHTTPHANDLERFACTORY interface is defined as follows: Interface IHTTPHANDLERFACTORY { IHTTPHANDLER GETHANDLER (HttpContext CTX, String RequestType, String URL, String pathtranslated; Void ReleaseHandler (IHTTPHANDLER HANDLER); } The GetHandler method is called at the beginning of the request, and ReleaseHandler is called at the end of the request, all of the Handler is called when it is needed. The process of using HttphandlerFactory is generally as follows: First define a class that actually handles httphandler, this class will be called in the handlerfactory to perform actual processing: Public Class Basichandler: IHTTPHANDLER {...} Then define your own handlerfactory: Public Class BasichandlerFactory: IHTTPHANDLERFACTORY { Public IHTTPHANDLER GETHANDLER (HTTPCONTEXT CTX, String RequestType, String URL, String pathtranslated) { Return New BasicHandler (); } Public void releasehandler (IHTTPHANDLER HANDAL) {} } Finally, register this Factory in the web.config file: TYPE = "MyNamespace.basicHandlerFactory, Myassembly" /> httphandlers> configure> Asynchronous Handler Asynchronous processing on HTTP requests can be implemented by implementing IHTTPASYNCHANDLER. IHTTPASYNCHANDLER interface inherits IHTTPHANDLER, but also requires the ProcessRequest method and isreusable attribute, and you need to implement the BeginProcessRequest and EndProcessRequest methods. BeGinProcessRequest starts asynchronous calls to handle individual HTTP requests, and EndProcessRequest executes cleaning code at the end of the process. IHTTPASYNCHANDLER's implementation and registration is similar to IHTTPHANDLER, and readers can refer to the MSDN's related documentation. Now, do you have a certain understanding of the concept and application of HTTP HANDLER? In the next article, we will mainly introduce the application of HTTP Module and give an instance of the HTTPModule implementation permission system.