Custom HTTP processing and application in ASP. NET

Source: Internet
Author: User
Tags microsoft iis

Custom HTTP processing and application in ASP. NET

Www.itonline.gd.cn 2003-12-11

 

When developing Microsoft IIS-based applications, developers can not only write ASP programs, but also use visual c ++ and other development tools to develop ISAPI applications for more powerful functions. You can write two types of ISAPI extensions: ISAPI server Extention and ISAPI filter. However, the compilation of ISAPI extended applications usually requires developers, development and deployment are difficult. In the development of ASP. NET application, we can still write the ISAPI application to expand the IIS function, however, Asp. NET provides us with another option-using HTTP handler and HTTP module. This is achieved through the ihttphandler and ihttpmodule interfaces. HTTP handler provides functions similar to ISAPI server extention, while httpmodule implements functions similar to ISAPI filter, and is much simpler than ISAPI in development and deployment. The application httphandler and httpmodule allow applications to interact with low-level requests and response services of IIS web servers. This article first introduces the concepts and basic usage of httphandler and httpmodule, and introduces a case of applying httpmodule to implement a permission system.
Basic Model of HTTP processing pipeline

 

To study httpmodule and ihttphandler, you must first have an understanding of ASP. NET processing pipelines. In ASP. in the. NET application, the system uses a set of related classes to process client requests (requests), Asp. NET application processing mode can be called HTTP processing pipeline. Httpmodule and ihttphandler are two processes in the processing pipeline. Classes in the HTTP processing pipeline are defined in the system. Web namespace, mainly including the following types :·
The httpworkerrequest abstract class defines ASP. NET page to process requests. · httpruntime provides a set of services for processing applications. · httpcontext stores all the context information related to processing a request; · httpapplicationfactory provides applications for related directories; · httpapplication defines all ASP. NET applications. This class is also in the user's global. the base class of the application defined in the asax file; · modules handles pre-and post-request events; · handlerfactories provides handlers in the application; · handlers processes requests and responses. The HTTP processing pipeline model is as follows:

 

 

Figure 1: HTTP MPs queue
On Windows, HTTP pipline requires IIS support. To run ASP.. NET application. IIS requires the following two files: aspnet_isapi.dll and aspnet_wp.exe · extension are an ISAPI extention. They will send requests to IIS and forward them to aspnet_wp.exe for processing. The process of processing requests using httpruntime is shown in the following figure:

 

 

Figure 2: HTTP MPs queue on IIS
 
Httphandler implementation
Httphandler implements functions similar to ISAPI extention. It processes request information and sends response ). The httphandler function is implemented through the ihttphandler interface. In fact, we are writing ASP. NET page, Asp. the base class inherited by the. NET page -- system. web. UI. page -- also implements the httphandler interface, which is also an httphandler. Let's take a look at its definition (C #):
Public class page: templatecontrol, ihttphandler
 

The ihttphandler interface is defined as follows:
Interface ihttphandler
{
Void processrequest (httpcontext CTX );
Bool isreuseable {Get ;}
}
 

In the API, processrequest adds your own code and processes it accordingly. The isreuseable attribute specifies whether the httphandler implementation class needs to be cached. The following example shows the basic usage of httphandler: 1. Create a project named mynamespace and add a class named myhandler. The Code is as follows:
Example 1:
Namespace mynamespace
{
Public class myhandler: ihttphandler
{
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 file; 3. Create a New webapplication project, or open a webapplication project. DLL is added to the reference of the project, or copied to the bin directory of the project; 4. Modify the web. config to add the following content:
<Configuration>
<System. Web>
<Httphndlers>
<Add verb = "*" Path = "*. aspx"
Type = "mynamespace. myhandr, mynamespace"/>
</Httphndlers>
</System. Web>
</Configuration>
 

Description of options in the configuration file: · verb can be "get" or "Post", indicating that the get or POST request is processed. "*" Indicates that all requests are processed. · Path indicates processing the corresponding file. "*. aspx" indicates processing the requests sent to all aspx pages. You can specify a path, such as "/test/*. aspx", indicating that the aspx file under the test directory is processed only. · In the type attribute, the string before the comma specifies the Class Name of the httphandler implementation class, and the subsequent string specifies the name of the DLL file. Currently, on any ASPX page in the request project, only one line of text is displayed:
This is my handler
 

Because our Custom Handler intercepts all requests sent to the ASPX page and uses its own method to process these requests. To make our ASPX page run smoothly, we need to modify the Web. config file:
<Configuration>
<System. Web>
<Httphndlers>
<Add verb = "*" Path = "*. foo"
Type = "mynamespace. myhandr, hander"/>
</Httphndlers>
</System. Web>
</Configuration>
 

In order to make requests for files with the suffix ". foo" be intercepted and run by our handler, we need some additional work. Open the IIS console, click the site, and select "properties". The site Properties dialog box is displayed. Select the Home Directory option. 3:

 

 

Figure 3: web site Properties dialog box
Select Configuration. The application configuration dialog box is displayed. Add ". foo" to application ing. 4:

 

 

Figure 4: Add application ing
Now we can add a. Foo file to the project. When sending a request to this file, the browser displays:
This is my handler
 

Access to other aspx files is not affected.
Implement handler Factory
Another option to implement the httphandler function is to implement a handler factory, which is implemented through 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 G ethandler method is called at the beginning of the request, while the releasehandler method is called at the end of the request and when no handler is needed. The process of using httphandlerfactory is generally as follows: First, define the class for actually processing httphandler. This class will be called in handlerfactory for 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 handler ){}
}
 

Finally, register the factory in the web. config file:
<Configuration>
<System. Web>
<Httphandlers>
<Add verb = "Post" Path = "*. foo"
Type = "mynamespace. basichandlerfactory, myassembly"/>
</Httphandlers>
</System. Web>
</Configuration>
 

 
Asynchronous Handler
Ihttpasynchandler can implement asynchronous processing of HTTP requests. The ihttpasynchandler interface inherits ihttphandler and must also implement the processrequest method and isreusable attributes. At the same time, the beginprocessrequest and endprocessrequest methods must be implemented. Beginprocessrequest starts an asynchronous call to process a single HTTP request, while endprocessrequest executes the cleaning code at the end of the process. The implementation and registration of ihttpasynchandler are similar to those of ihttphandler. You can refer to the relevant documentation of msdn. Do you know the concept and application of HTTP handler? In the next article, we will mainly introduce the application of HTTP module and provide an example of using httpmodule to implement the permission system.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.