在ASP.Net2.0中使用UrlRewritingNet实现链接重写

在ASP.Net2.0中使用UrlRewritingNet实现链接重写

很多时候我们需要链接转向(Url Rewriting),例如二级域名转向、文章访问链接等场合。

让我们看两个例子:

1 你现在看到的当前作者的博客园的域名:
http://deymmtd.cnblogs.com 实际上是 http://www.cnblogs.com/ 的一种链接重写(Url Rewriting)。

2 codeproject上的文章,我们获取的地址都是以文章主要关键字做的网页名称:
http://www.codeproject.com/cs/webservices/wsdlparser.asp 
个人觉得实际的文章链接可能是如下格式(纯属猜想)
http://www.codeproject.com/news.asp?id=123456  


那么如何在Asp.net2.0中如何实现链接重写呢?
可以采用UrlRewritingNet.UrlRewriter.dll来轻松实现
UrlRewritingNet.UrlRewriter.dll 可从其官方网站下载:
http://www.urlrewriting.net

我们要做的就是进行Web.Config文件的设置

0 设置之前需要添加引用:UrlRewritingNet.UrlRewriter.dll
1 <configSections>间添加:
  <configSections>
  <section name="urlrewritingnet" restartOnExternalChanges="true" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter"/>
 </configSections>
2   添加httpModules
  <httpModules>
      <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>
  </httpModules>
3 添加链接重写的设置
 <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>

完整的一个Web.Config文件

<? 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 >
deymmtd

你可能感兴趣的:(asp.net)