URL rewriting in ASP. NET (4)

Source: Internet
Author: User

& #8226; dispose (). This method is called when the request is complete and has been sent back to IIS. You shall perform all final cleanup operations here.

To create an HTTP module for URL rewriting, I will start with creating an abstract base class basemodulerewriter. This class implements ihttpmodule. In the init () event, it binds the authorizerequest event of httpapplication to the basemodulerewriter_authorizerequest method. The basemodulerewriter_authorizerequest method calls the rewrite () method of the requested path and the httpapplication object of the init () method. The rewrite () method is abstract. That is to say, in the basemodulerewriter class, the rewrite () method does not have a method subject. Classes derived from basemodulerewriter must overwrite the method and provide the method subject.

With this base class, you only need to create a class derived from basemodulerewriter, which can overwrite rewrite () and execute the URL rewriting logic there. The following shows the basemodulerewriterCode.

Public abstract class basemodulerewriter: ihttpmodule
{
Public Virtual void Init (httpapplication APP)
{
// Warning! This code is not applicable to Windows Authentication!
// If Windows authentication is used,
// Change to app. beginrequest.
App. authorizerequest + = new
Eventhandler (this. basemodulerewriter_authorizerequest );
}

Public Virtual void dispose (){}

Protected virtual void basemodulerewriter_authorizerequest (
Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
Rewrite (App. Request. Path, APP );
}

Protected abstract void rewrite (string requestedpath,
Httpapplication APP );
}

Note that the basemodulerewriter class will execute URL rewriting in the authorizerequest event. As mentioned above, if you use Windows Authentication in combination with file authorization, you need to make changes so that you can perform URL rewriting in the beginrequest or authenticaterequest event.

The modulerewriter class extends the basemodulerewriter class and is responsible for performing actual URL rewriting. Modulerewriter contains a single overwrite () method, as shown below:

Protected override void rewrite (string requestedpath,
System. Web. httpapplication APP)
{
// Obtain configuration rules
Rewriterrulecollection rules =
Rewriterconfiguration. getconfig (). Rules;

// Traverse each rule...
For (INT I = 0; I <rules. Count; I ++)
{
// Obtain the search mode and
// Parse the URL (convert to the corresponding directory)
String lookfor = "^" +
Rewriterutils. resolveurl (App. Context. Request. applicationpath,
Rules [I]. lookfor) + "$ ";

// Create a RegEx (note that ignorecase... has been set ...)
RegEx Re = new RegEx (lookfor, regexoptions. ignorecase );

// Check whether matching rules are found
If (Re. ismatch (requestedpath ))
{
// Matched rule found -- make necessary replacement
String sendtourl =
Rewriterutils. resolveurl (App. Context. Request. applicationpath,
Re. Replace (requestedpath, rules [I]. sendto ));

// Rewrite the URL
Rewriterutils. rewriteurl (App. Context, sendtourl );
Break; // exit the For Loop
}
}
}

The rewrite () method starts from obtaining a set of rewrite rules in the web. config file. Then, it will traverse the rewrite rule and traverse one at a time. For each rule, it will obtain the lookfor attribute of the rule, the regular expression is used to determine whether matching rules are found in the requested URL.

If a matching rule is found, replace the regular expression in the requested path with the value of sendto. Then, the replaced URL is passed to the rewriterutils. rewriteurl () method. Rewriterutils is a helper class, which provides a pair of URL rewriting HTTP module and HTTP ProcessingProgramStatic method used. The rewriterurl () method only calls the rewriteurl () method of the httpcontext object.

Note: You may have noticed that rewriterutils. resolveurl () will be called during regular expression matching and replacement (). This helper method only replaces all ~ Instance.

The whole code of the URL rewriting engine can be downloaded with this article. We have introduced most closely related components, but there are some other components (for example, for web. the XML format rewriting rules in the config file are deserialized to make it the class of the object), and the HTTP handler factory for URL rewriting. The remaining three parts of this article will introduce the actual usage of URL rewriting.

Back to Top
Use the URL rewriting engine to perform simple URL rewriting
To demonstrate the URL rewriting engine, we will build an ASP. NET web application that uses simple URL rewriting. Assume that the company we work for sells classified products through the network. These products are divided into the following categories:

Category id category name
1
Beverage

2
Condiments

3
Candy

4
Dairy products

...
...

Suppose we have created an ASP. NET webpage named listproductsbycategory. aspx. This webpage accepts the category id value in the query string and displays all products of this category. Therefore, users who want to view the drinks we sell can access listproductsbycategory. aspx? Categoryid = 1, and users who want to view dairy products can access listproductsbycategory. aspx? Categoryid = 4. In addition, suppose we have a page named listcategories. aspx, which lists all product categories for sale.

obviously, this is an example of URL rewriting, because the URLs provided to users do not make any sense to users, nor provide them with any "deletable ". Therefore, let's use URL rewriting so that when users access/products/beverages. aspx, their URL will be rewritten to listproductsbycategory. aspx? Categoryid = 1. We can use the following URL rewriting rules in the web. config file to implement this function.

Related Article

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.