ASP. NET 2.0 URL rewrite learning Summary

Source: Internet
Author: User

When I read the book ASP. NET 2.0 programming Pearl, I learned about URL rewrite.

The examples in this book aim to achieve URL rewrite by modifying web. config. There are still many components and methods that can achieve this function through self-search on the Internet. I learned only two ways for time. One is in ASP. NET 2.0 programming, and the other is implemented through the urlrewriter of Ms.

1,

Add a configuration node in <system. Web> </system. Web>.

<System. Web>
<Urlmappings enabled = "true">
<Add url = "~ /2006"
Mappedurl = "~ /Chapter1/yearview. aspx? Year = 2006 "/>
<Add url = "~ /2005"
Mappedurl = "~ /Chapter1/yearview. aspx? Year = 2005 "/>
<Add url = "~ /2006/01"
Mappedurl = "~ /Chapter1/monthview. aspx? Year = 2006 & amp; month = 01 "/>
<Add url = "~ /2006/02"
Mappedurl = "~ /Chapter1/monthview. aspx? Year = 2006 & amp; month = 02 "/>
<Add url = "~ /2005/01"
Mappedurl = "~ /Chapter1/monthview. aspx? Year = 2005 & amp; month = 01 "/>
<Add url = "~ /2005/02"
Mappedurl = "~ /Chapter1/monthview. aspx? Year = 2005 & amp; month = 02 "/>
</Urlmappings>
</System. Web>

Then, the front-end client can add a hyperlink similar

<Asp: hyperlink id = "hyperlink1" navigateurl = "~ /2005 "runat =" server "> 2005 </ASP: hyperlink>
<Asp: hyperlink id = "hyperlink1" navigateurl = "~ /2005 "runat =" server "> 2005 </ASP: hyperlink>
<Asp: hyperlink id = "hyperlink1" runat = "server" navigateurl = "~ /Year/01 "> 01 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink2" runat = "server" navigateurl = "~ /Year/02 "> 02 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink3" runat = "server" navigateurl = "~ /Year/03 "> 03 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink4" runat = "server" navigateurl = "~ /Year/04 "> 04 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink5" runat = "server" navigateurl = "~ /Year/05 "> 05 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink6" runat = "server" navigateurl = "~ /Year/06 "> 06 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink7" runat = "server" navigateurl = "~ /Year/07 "> 07 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink8" runat = "server" navigateurl = "~ /Year/08 "> 08 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink9" runat = "server" navigateurl = "~ /Year/09 "> 09 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink10" runat = "server" navigateurl = "~ /Year/10 "> 10 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink11" runat = "server" navigateurl = "~ /Year/11 "> 11 </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyperlink12" runat = "server" navigateurl = "~ /Year/12 "> 12 </ASP: hyperlink> <br/>

When you click a hyperlink, only http: // localhost: 2537/webdemo/2005/01 will be displayed in the address bar. In fact, it is connected to http: // localhost: 2537/webdemo, Chapter1/monthview. aspx? Year = 2006 & amp; month = 01

2,

First: Now msdnurlrewriting. After the installation is complete, findActionlessform, urlrewriter. Actionlessform. dll andUrlrewriter. dll. Then add it to your project.

2. modify the configuration file

Add the following nodes in <configuration> </configuration>:

<Configsections>
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</Configsections>
<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> ~ /(\ D {4})/(\ D {2})/(\ D {2}) </lookfor>
<Sendto> ~ /Guantesturlrewrit/showblogcontent. aspx? Year = $1 & amp; month = $2 & amp; Day = $3 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /(\ D {4})/(\ D {2})/(\ D {2})/Default \. html </lookfor>
<Sendto> ~ /Guantesturlrewrit/showblogcontent. aspx? Year = $1 & amp; month = $2 & amp; Day = $3 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /(\ D {4})/(\ D {2})/Default \. html </lookfor>
<Sendto> <! [CDATA [~ /Guantesturlrewrit/showblogcontent. aspx? Year = $1 & month = $2]> </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /(\ D {4})/Default \. html </lookfor>
<Sendto> ~ /Guantesturlrewrit/showblogcontent. aspx? Year = $1 </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>

Add the following nodes in <system. Web> </system. Web>.

<Httpmodules>
<Add type = "urlrewriter. modulerewriter, urlrewriter" name = "modulerewriter"/>
</Httpmodules>

Now the configuration file has been modified. Below is the clientCode:

<Asp: hyperlink id = "hyyear" navigateurl = "2003/default.html" runat = "server" width = "110px"> year </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyyearandmonth" navigateurl = "2003/12/default.html" runat = "server" width = "110px"> year and month </ASP: hyperlink> <br/>
<Asp: hyperlink id = "hyyearmonthandday" navigateurl = "2003/12/21/default.html" runat = "server" width = "247px"> year month and day </ASP: hyperlink> <br/>

In this way, when we click the button, we will find the real path through the regular expression in the configuration file.

For example, we click http: // localhost: 12003/demo/2003/default.html. The actual path is: http: // localhost: 12003/demo/guantesturlrewrit/showblogcontent. aspx? Year = 2003

The latter is relatively flexible and can be applied with regular expressions.

 

Author: Ice dish
Source: http://www.cnblogs.com/icebutterfly/
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the author's consent; Article The original text connection is provided; otherwise, the legal liability is required.

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.