Use urlrewritingnet in ASP. net2.0 to implement link Rewriting

Source: Internet
Author: User
In many cases, we need URL rewriting, for example, second-level domain name redirection, Article Access Links.

Let's look at two examples:

1. The domain name of the current author's blog Garden:
Http://heekui.cnblogs.comActuallyHttp://www.cnblogs.com/heekuiURL rewriting ).

2. for articles on codeproject, the addresses we get are all webpage names based on the main keyword of the article:
Http://www.codeproject.com/cs/webservices/wsdlparser.asp
I personally think the actual article link may be in the following format (purely conjecture)
Http://www.codeproject.com/news.asp? Id = 123456

So how can I rewrite links in ASP. net2.0?
You can use urlrewritingnet. urlrewriter. DLL to easily implement
Urlrewritingnet. urlrewriter. dll can be downloaded from its official website:Http://www.urlrewriting.net

All we need to do is set the Web. config file.

0. Add reference before setting: Urlrewritingnet. urlrewriter. dll
1 <configsections>:
<Configsections>
<Section name = "urlrewritingnet" restartonexternalchanges = "true" requirepermission = "false" type = "urlrewritingnet. configuration. urlrewritesection, urlrewritingnet. urlrewriter"/>
</Configsections>
2 addHttpmodules
<Httpmodules>
<Add name = "urlrewritemodule" type = "urlrewritingnet. Web. urlrewritemodule, urlrewritingnet. urlrewriter"/>
</Httpmodules>
3. Add link rewriting settings
<Urlrewritingnet rewriteonlyvirtualurls = "true" contextitemsprefix = "querystring" defaultpage = "default. aspx" defaultprovider = "RegEx" xmlns =" Http://www.urlrewriting.net/schemas/config/2006/07 ">
<Rewrites>
<Add name = "rewrite1" virtualurl = "^ HTTP \: // (. *)/urlrewritingtest/test. aspx" rewriteurlparameter = "excludefromclientquerystring" destinationurl = "~ /Default. aspx "rewrite =" Domain "ignorecase =" true "/>
<Add name = "rewrite2" virtualurl = "^ HTTP \: // (. *)/urlrewritingtest/test1.aspx" rewriteurlparameter = "excludefromclientquerystring" destinationurl = "~ /Info. aspx? Year = 2007 & amp; month = 3 & amp; Day = 9 "rewrite =" Domain "ignorecase =" true "/>
</Rewrites>
</Urlrewritingnet>

A complete Web. config file <? XML version = "1.0" ?>
< Configuration >
< Configsections >
< Section Name = "Urlrewritingnet" Restartonexternalchanges = "True" Requirepermission = "False" Type = "Urlrewritingnet. configuration. urlrewritesection, urlrewritingnet. urlrewriter" />
</ Configsections >
< Urlrewritingnet Rewriteonlyvirtualurls = "True" Contextitemsprefix = "Querystring" Defaultpage = "Default. aspx" Defaultprovider = "RegEx" Xmlns = "Http://www.urlrewriting.net/schemas/config/2006/07" >
< Rewrites >
< Add Name = "Rewrite1" Virtualurl = "^ HTTP \: // (. *)/urlrewritingtest/test. aspx" Rewriteurlparameter = "Excludefromclientquerystring" Destinationurl = "~ /Default. aspx" Rewrite = "Domain" Ignorecase = "True" />
< Add Name = "Rewrite2" Virtualurl = "^ HTTP \: // (. *)/urlrewritingtest/test1.aspx" Rewriteurlparameter = "Excludefromclientquerystring" Destinationurl = "~ /Info. aspx? Year = 2007 & amp; month = 3 & amp; Day = 9" Rewrite = "Domain" Ignorecase = "True" />
< Add Name = "Rewrite3" Virtualurl = "^ HTTP \: // (. *)/urlrewritingtest/test (\ D {4}) (\ D {2}) (\ D {2}). aspx" Rewriteurlparameter = "Excludefromclientquerystring" Destinationurl = "~ /Info. aspx? Year = $2 & amp; month = $3 & amp; Day = $4" Rewrite = "Domain" Ignorecase = "True" />
< Add Name = "Rewrite4" Virtualurl = "^ HTTP \://(. *)/urlrewritingtest/(\ D {4})/(\ D {2})/(\ D {2})/info. aspx" Rewriteurlparameter = "Excludefromclientquerystring" Destinationurl = "~ /Info. aspx? Year = $2 & amp; month = $3 & amp; Day = $4" Rewrite = "Domain" Ignorecase = "True" />
< Add Name = "Rewrite5" Virtualurl = "^ HTTP \: // (. *)/urlrewritingtest/stock (\ D {6}). aspx" Rewriteurlparameter = "Excludefromclientquerystring" Destinationurl = "~ /Stockinfo. aspx? Code = $2" Rewrite = "Domain" Ignorecase = "True" />
</ Rewrites >
</ Urlrewritingnet >
< Appsettings />
< System . Web >
< Httpmodules >
< Add Name = "Urlrewritemodule" Type = "Urlrewritingnet. Web. urlrewritemodule, urlrewritingnet. urlrewriter" />
</ Httpmodules >
< Compilation Debug = "True" />
</ System. Web >
</ Configuration >

Example Program
We made a query string input to display the corresponding date page info. aspx Protected   Void Page_load ( Object Sender, eventargs E)
{
String Stryear = Request. querystring [ " Year " ]. Tostring ();
String Strmonth = Request. querystring [ " Month " ]. Tostring ();
String Strday = Request. querystring [ " Day " ]. Tostring ();

Response. Write ( String . Format ( " The date you entered is {0}-{1}-{2} " , Stryear, strmonth, strday ));
}

General access method:Http: // localhost/urlrewritingtest/info. aspx? Year = 2007 & month = 03 & day = 08

Page Rewriting Method 1: http: // localhost/urlrewritingtest/test20070308.aspx

Page Rewriting Method 2: http: // localhost/urlrewritingtest/2007/03/08/info. aspx

You can see that the actual access results are consistent.

The example file also contains an example of Stock Information Viewing:
Http: // localhost/urlrewritingtest/stock600616.aspx = http: // localhost/urlrewritingtest/stockinfo. aspx? Code = 600616

Download the sample program:/files/heekui/urlrewritingtest.rar

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.