[Learning] Using HttpModules to override URLs

Source: Internet
Author: User

It is said that through HttpModules can be like: http://www.infotouch.cn/detail.aspx? The URL address of id = 120 is rewritten to: http://www.infotouch.cn/detail/120.aspx. The most direct advantage is that the search engine can search for pages, because the search engine is? The subsequent parameters are ignored.

Today, I tried to find out that HttpModules can only rewrite URLs with specific extensions (note: only "Asp ing"-"application extension" can be assigned to Asp.. Net). I found some English documents from Google and found that this situation is determined by the mechanism of IIS processing requests.

How IIS processes requests for URLs without extensions:

If the request path has an extension, IIS first checks whether the corresponding application extension has been set. If yes, it gives control to the application:
So for http://www.infotouch.cn/detail.aspx? It's easy to handle situations like id = 120, as long as it's handled as a http://www.infotouch.cn/detail/120.aspx. This is because IIS handed over control of *. aspx extensions to Asp. Net and then to HttpModules.

If the requested path (path) has no extension, such as a path like a http://www.infotouch.cn/detail. IIS first checks whether the virtual path corresponds to a local directory. If a local directory exists, it checks whether the directory has a default file. If yes, the default file path. Otherwise, IIS reports an Http404-File Failure error.

Practice:

First, write a class for processing URLs rewriting, and this class must inherit the IHttpHandler interface. Take the blog garden program as an 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.

In the content_BeginRequest method, the specific processing of URL rewriting details, such as, will the http://www.cnblogs.com/archive.aspx? User = rrooyy & id = 56041 is rewritten as callback. 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.

Source: http://www.cnblogs.com/154691780/archive/2008/01/17.html

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.