Principles and Applications of httphandler, httpmodule, and ihttphandlerfactory in Asp.net (I ))

Source: Internet
Author: User

Recently, I was interested in the page lifecycle and read some articles written by some cool people.Article, I feel like I do not understand (I still need to continue to work hard ^_^ ).
Several important objects are involved in the lifecycle: httphandler, httpmodule, ihttphandlerfactory, and their execution (sequence) it has a great deal of connection with IIS (this article will be completed later ).

The general execution process is as follows:The client sends a page request, which is intercepted by a certain IIS process. It calls different pages for Processing Based on the requested page suffix (. aspx ).Program(. Asp> Asp. DLL ;. aspx-> ISAPI. DLL ). in the process of page processing, the page processing program must go through the process of httpmodule and httphandler: the former is used to process some events before and after page processing, and the latter is used to process real pages.
As mentioned above, httpmodule processes pages before and after page processing, so it does not affect real page requests. It is usually used to add some information (such as copyright notice) to the header or tail of each page. I have seen some free space. After uploading our pages, I found that there were many small advertisements in the header and tail of each page ...., if you understand the principles of httpmodule, it is not very difficult to do this ~
1. Create a website
2. Create a new class named httpmoduletest. inherit the ihttpmodule interface in this class. Check that the ihttpmodule mainly includes the init () and dispose () methods ().

// Class: httpmoduletest. CS
// ---------------------------------------------
Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;

/**/ /// <Summary>
/// Brief description of httpmoduletest
/// </Summary>
Public Class Httpmoduletest: ihttpmodule
... {
// The init method is used to register an httpapplication event.
Public Void Init (httpapplication Application)
... {
Application. beginrequest
+ =
(
New Eventhandler ( This . Application_beginrequest ));
Application. endrequest
+ =
(
New Eventhandler ( This . Application_endrequest ));
}


// Execute this event method before page processing
Private Void Application_beginrequest (Object source,
Eventargs E)
... {
Httpapplication
= (Httpapplication) source;
Httpcontext Context
= Application. context;
Context. response. Write (
" <H1> <font color = Red> helloworldmodule: Beginning of request </font> " );
Context. response. Write (
" <A href = 'HTTP: // www.baidu.com '> <H2> <font color = blue> click here you will see some ads </font> </H2> </a> " );
}


// Execute this event method before page processing
Private Void Application_endrequest (Object source, eventargs E)
... {
Httpapplication
= (Httpapplication) source;
Httpcontext Context
= Application. context;
Context. response. Write (
" <HR> " );
}


// Resource release
Public Void Dispose ()
... {
}

}

// -----------------------------------------------

3. Compile this class into a DLL:
Run the vs2005 command line tool: Enter CSC/T: Library/R: system. web. dll d: \ demo \ httpcontextdemo \ app_code \ httpmoduletest. CS and then locate httpmoduletest under D: \ Program Files \ Microsoft Visual Studio 8 \ Vc. DLL, copy it to the bin directory of the current project.
4. Configure web. config so that the handler can find our custom httpmodule>
Add:

< Httpmodules >
< Add Name = "Test" Type = "Httpmoduletest, httpmoduletest" />
</ Httpmodules >

Note: The name is a casual name. The first half of the type is the class name implementing the ihttpmodule, and the second half is the name of the processing file (. dll name ).
5. Create a page for running.
You will find that no matter which page you are running, there will be additional custom information in the header and tail except the page body.

==============
Of course, this is just a demo. In actual applications, the module can access the context of the current request. We can perform any operations on the request according to the context information.

Public Class Ipfiltermodule: ihttpmodule
... {
Public Void Init (httpapplication context)
... {
Context. beginrequest
+ = New Eventhandler (context_beginrequest );
}


Void Context_beginrequest ( Object Sender, eventargs E)
... {
// In the event processing process, we can obtain the context of the current request. Based on the Request context, we can
// Request to perform any operation

Httpapplication app
= Sender As Httpapplication; // Obtain the current application
If (App ! = Null )
... {
// App. Context: Get the current context information
String Hostname = App. Context. Request. userhostname;

If (Hostname ! = " CC " )
... {
App. Context. response. statuscode
= 404 ;
App. Context. response. End ();
}

}

}

Public Void Dispose ()
... {

}

}


To be continued ......

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.