The ihttpmodule provides the implementation class with module initialization and event handling.
Ihttpmodule contains two methods:
Public void Init (httpapplication context );
Public void dispose ();
Init (): This method accepts an httpapplication object. httpapplication represents the current application.ProgramIn this method, we need to register the event that the httpapplication object exposes to the client. It can be seen that this method is only used to register events, and the actual event processing program requires us to write another method.
The entire process is well understood:
1. When the first resource of the site is accessed, Asp. net will create an httpapplication class instance, which represents the site application, at the same time will create all in the web. the module instance that has been registered in config.
2. The init () method of modul is called when a module instance is created.
3. Register the events exposed by the httpapplication that you want to respond to in the init () method. (Only simple registration of methods is required. The actual method needs to be written separately ).
4. Httpapplication Events are triggered during the application cycle.
5. When an event is triggered, call the method that the module has registered in its Init () method.
Dispose (): It can be cleaned up before garbage collection.
public class moduledemo: ihttpmodule
{< br> Public void Init (httpapplication context) {
// Registration httpapplication application beginrequest event
// it can be any other httpapplication exposed event
context. beginrequest + = new eventhandler (context_beginrequest);
}
Void context_beginrequest (Object sender, eventargs e ){
Httpapplication application = (httpapplication) sender;
Httpcontext context = application. context;
//Do some practical work,HttpcontextAll objects are obtained, and the rest can be used freely.
}
Public void dispose (){
}
}
PassHTTP ModuleDirectionHTTPWrite text in the request output stream
In this example, we only use the beginrequest event and endrequest event to describe how to use the HTTP module. This example shows how to use the HTTP module.
First, create a new site and add the class file: moduledemo. CS: To the app_code directory:
Public class moduledemo: ihttpmodule
{
// InitThe method is only used to register the method for the expected event.
Public void Init (httpapplication context ){
Context. beginrequest + = new eventhandler (context_beginrequest );
Context. endrequest + = new eventhandler (context_endrequest );
}
// processing beginrequest actual Code
void context_beginrequest (Object sender, eventargs E) {
httpapplication application = (httpapplication) sender;
httpcontext context = application. context;
context. response. write ("
from httpmodule processing, request arrival
");
}
//ProcessingEndrequestActual Event code
Void context_endrequest (Object sender, eventargs e ){
Httpapplication application = (httpapplication) sender;
Httpcontext context = application. context;
Context. response. Write ("<HR> <H1 style = 'color: # f00'>FromHttpmoduleProcessing, the request ends</H1> ");
}
Public void dispose (){
}
}
The above code is very simple. It registers the beginrequest event and endrequest event of the httpapplication instance. The event processing method is only used at the beginning and end of the HTTP request, write different content to the input stream of the HTTP request.
Next, write the following content in the system. Web node of Web. config:
<System. Web>
<Httpmodules>
<Add name = "mymodule" type = "moduledemo"/>
</Httpmodules>
</System. Web>
In our own HTTP module, assume that the class is named moduledemo, which is located in the mynamespace namespace and the Assembly name is mydll. the DLL is copied to the bin directory and stored in the web. config file system. create an httpmodules node under a web node:
<System. Web>
<Httpmodules>
<Add name = "custommodulename" type = "mynamespace. moduledemo, mydll"/>
</Httpmodules>
</System. Web>
TypeThe attributes are divided into two parts: the namespace and the class name, that is, the type name, and the Assembly name. If we create the code in the app_code directory, you do not need to specify the Set Name.
NameThe attribute is named by ourselves, not necessarily the same as the class name. Here I name it "mmmodulename ". We can get the httpmodulecollection through the modules attribute of the application (httpapplication), and then get the httpmodule object through the name attribute.
Through the name attribute, we can also. in the asax file, compile the event processing program exposed by the custom httpmodule. The format is void modulename_eventname (Object sender, eventargs E ).