How does httpmodule work?
When an http request arrives at httpmodule, the entire asp tutorial. the. net framework system has not processed this http request. That is to say, httpmodule is the "Only Way" for an http request ", therefore, some required information can be appended to the http request information before the http request is passed to the real request processing center (httphandler, you can also perform some additional work on the intercepted http request information, or simply terminate the http request that meets certain conditions in some cases, so that it can act as a filter.
Using system;
Using system. collections. generic;
Using system. text;
Using system. web;
Namespace myhttpmodule
{
/// <Summary>
/// Description: Used to implement your own httpmodule class.
/// </Summary>
Public class myfirsthttpmodule: ihttpmodule
{
Private void application_beginrequest (object sender, eventargs e)
{
Httpapplication application = (httpapplication) sender;
Httpcontext context = application. context;
Httprequest request = application. request;
Httpresponse response = application. response;
Response. write ("I am from beginrequest in custom httpmodule <br/> ");
}
Private void application_endrequest (object sender, eventargs e)
{
Httpapplication application = (httpapplication) sender;
Httpcontext context = application. context;
Httprequest request = application. request;
Httpresponse response = application. response;
Response. write ("I am from the endrequest in the Custom httpmodule <br/> ");
}
# Region ihttpmodule Member
Public void dispose ()
{
}
Public void init (httpapplication application)
{
Application. beginrequest + = new eventhandler (application_beginrequest );
Application. endrequest + = new eventhandler (application_endrequest );
}
# Endregion
}
}
Httpmodule permission management
//// The following content in web. config
<Httpmodules>
<Add name = "scriptmodule" type = "system. web. handlers. scriptmodule,
System. web. extensions, version = 3.5.0.0, culture = neutral,
Publickeytoken = 31bf3856ad364e35 "/>
<Add name = "mymodule" type = "mymodule. mymodule"/>
<! -- <Add name = "mymodule" type = ""/> -->
</Httpmodules>
/// This method is called every execution page.
. Net code
Using system. configuration;
Using system. linq;
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system.web.ui.html controls;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system. xml. linq;
/// <Summary>
/// Summary of class1
Namespace mymodule
{
Public class mymodule: ihttpmodule
{
/// <Summary>
/// Initialize and add two events to the application.
/// </Summary>
/// <Param name = "application"> </param>
Public void init (httpapplication application)
{
Application. beginrequest + = (new
Eventhandler (this. application_beginrequest ));
Application. endrequest + = (new
Eventhandler (this. application_endrequest ));
}
/// Here it must be equivalent to converting response. It cannot be used directly here. I don't know why.
/// Write this statement to determine the permission.
Private void application_beginrequest (object source, eventargs e)
{
Httpapplication application = (httpapplication) source;
Httpresponse responseo tutorial k = application. context. response;
Responseok. write ("
}
Private void application_endrequest (object source, eventargs e)
{
Httpapplication application = (httpapplication) source;
Httpresponse responseok = application. context. response;
Responseok. write ("}
Public void dispose (){}
}
}