HTTP processing and application customization in ASP. NET httpmodule

Source: Internet
Author: User

Httphandler implements functions similar to ISAPI extention. It processes request information and sends response ). The httphandler function is implemented through the ihttphandler interface. Httpmodule implements functions similar to ISAPI filter.

 

Httpmodule implementation

Httpmodules implements functions similar to ISAPI filter. Generally, the following steps are required during development:

1. Write a class to implement the ihttpmodule Interface

2. Implement the init method and register the required Method

3. Registration Method

4. Implement the dispose method. If you need to manually clear the class, you can add the implementation of the dispose method, but this is not necessary. Generally, you can not add anyCode.

5. register the class you have written in the web. config file.

The following is an example of httpmodules. In this example, only the beginrequest and endrequest events of httpapplication are registered, and relevant information is printed out through the implementation of these events.

Example 1:
Using system;
Using system. Web;
Namespace mymodule
{
Public class mymodule: ihttpmodule
{
Public void Init (httpapplication Application)
{
Application. beginrequest + = (New
Eventhandler (this. application_beginrequest ));
Application. endrequest + = (New
Eventhandler (this. application_endrequest ));
}
Private void application_beginrequest (Object source, eventargs E)
{
Httpapplication application = (httpapplication) source;
Httpresponse response = application. Context. response;
Response. Write ("}
Private void application_endrequest (Object source, eventargs E)
{
Httpapplication application = (httpapplication) source;
Httpresponse response = application. Context. response;
Response. Write ("}
Public void dispose ()
{
}
}
}

ProgramREFERENCE The following namespace:

Using system;
Using system. Web;

Because classes such as httpapplication, httpcontext, and httpresponse are defined in system. Web, system. Web namespaces must be referenced.

The mymodule class implements the ihttpmodule interface. The init method specifies the method for implementing the beginrequest and endrequest events. In the two methods, some information is simply printed separately.

Next, register this class in the web. config file and you can use this httpmodule. The registration method is as follows:

<Configuration>
<System. Web>
<Httpmodules>
<Add name = "mymodule" type = "mymodule, mymodule"/>
</Httpmodules>
</System. Web>
</Configuration>

Now let's take a look at the effect. Compile An ASPX page test. aspx with the following content:

<%
Response. Write ("%>

After running:

 

 

 

In-depth research on httpmodule

Httpmodule processes a series of events of the httpapplication object to influence the HTTP processing pipeline. These events are registered in the init method of httpmodule, including:

Beginrequest
Authenticaterequest
Authorizerequest
Resolverequestcache
Acquirerequeststate
Prerequesthandlerexecute
Postrequesthandlerexecute
Releaserequeststate
Updaterequestcache
Endrequest

Some of the events correspond to events in global. asax. The corresponding relationship is as follows:

 

Httpmodule global. asax
Beginrequest application_beginrequest
Authenticaterequest application_authenticaterequest
Endrequest application_endrequest

 

In example 1, The beginrequest and endrequest events are processed. The processing methods for other events are similar.

According to httphandler, some of these events occur before httphandler, and some occur after httphandler completes processing. It is very important to understand the sequence of events, because the objects on the server have different performance in different time periods. One example is the use of session. Not all events can process sessions, but only a limited number of events. For detailed procedures, refer to the following HTTP request processing lifecycle diagram.

 

 

 

Use httpmodule to implement the Permission System

When developing an application system, permission control of the application system is very important. In ASP, permission control is troublesome, because we must add permission control code to each ASP page that requires permission control to control access to the page. In this case, apart from writing a large number of repeated generations?

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.