Using httpmodules to override URLs (Practice)

Source: Internet
Author: User
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.

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.