随笔-101  评论-391  文章-1 

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

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

让我们看两个例子:

1 你现在看到的当前作者的博客园的域名:
http://heekui.cnblogs.com 实际上是 http://www.cnblogs.com/heekui 的一种链接重写(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>

示例程序
我们做了一个按查询字符串的输入显示对应日期的页面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("你输入的日期是{0}年{1}月{2}日",strYear,strMonth,strDay));
    }

常规访问方式:http://localhost/UrlRewritingTest/info.aspx?year=2007&month=03&day=08


页面重写方式1:http://localhost/UrlRewritingTest/test20070308.aspx


页面重写方式2:http://localhost/UrlRewritingTest/2007/03/08/info.aspx


大家可以看到实际的访问效果是一致的。

示例文件中还有一个股票信息查看的例子:
http://localhost/UrlRewritingTest/stock600616.aspx = http://localhost/UrlRewritingTest/stockinfo.aspx?code=600616

示例程序下载:/Files/heekui/UrlRewritingTest.rar
posted on 2007-03-09 15:22 网际浪人 阅读(...) 评论(...) 编辑 收藏