Using httpmodules to override URLs (Practice)
First, write a class for processing URLs rewriting, and this class must inherit the ihttphandler interface.ProgramFor example:
Public class urlrewritemodule: system. Web. ihttpmodule
{
Public void Init (httpapplication context)
{
Context. beginrequest + = new eventhandler (context_beginrequest );
}
Public void dispose ()
{
}
}
The urlrewritemodule class is the class for processing URLs rewriting. It inherits the ihttphandler interface and implements the two methods of this interface, init and dispose. Register your own method in the init method, as shown in the example above:
Content. beginrequest + = new eventhandler (content_beginrequest );
Beginrequest is an event that is triggered when a new HTTP request is received. content_beginrequest is the method for processing the request. In addition, there are many ways to register httpmodules, such as endrequest, error, disposed, and presendrequestcontent.
The content_beginrequest method processes the details of URL rewriting. For example Http://www.cnblogs.com/rrooyy/archive/2004/10/24/56041.html override to http://www.cnblogs.com/archive.aspx? User = rrooyy & id = 56041 (Note: I did not carefully read the Dudu program. Here is just an example ). Then rewrite the generated URL with the httpcontext. rewritepath () method, as shown below:
Private void context_beginrequest (Object sender, eventargs E)
{
Httpcontext context = (httpapplication) sender). context;
// Obtain the old URL
String url = context. Request. Path. tolower ();
// Generate a new URL
String newurl =...; // detailed process
// Rewrite the URL
Context. rewritepath (newurl );
}
Reminder:The newurl format is not.
Finally, register the URL rewriting class in Web. config. The format is as follows:
<Httpmodules>
<Add type ="Classname, assemblyname"Name ="Modulename"/>
<Remove name ="Modulename"/>
<Clear/>
</Httpmodules>
You can use the <add> label to register a class. <remove> you can remove a class. If a subdirectory does not want to inherit an HTTP module from the parent directory, you need to use this label; <clear/> All HTTP module registrations can be removed.