在ASP.Net2.0中使用UrlRewritingNet实现链接重写 - Web.网际浪...yyyyy

来源:百度文库 编辑:神马文学网 时间:2024/04/29 08:25:37
在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 间添加:
  
  

 
2   添加httpModules
 

      
  

3 添加链接重写的设置
 http://www.urlrewriting.net/schemas/config/2006/07
">
  
   
   
  

 

完整的一个Web.Config文件


    
        
    
    
        
            
            
            
            
      
        
    
    
    
        
            
        
        
  


示例程序
我们做了一个按查询字符串的输入显示对应日期的页面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