VS2005网站内实现地址重定向的方法。

Web.Config文件中添加代码段:
<configuration>
<configSections>        

    
<sectionGroup name="blogConfig">
      
<section name="blogConfigDictionary" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    
</sectionGroup>
    
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
 
</configSections>
<RewriterConfig>
    
<Rules>
      
<!-- Blog 内容显示程序规则 -->
      
<RewriterRule>
        
<LookFor>~/(\d{4})/(\d{2})/(\d{2})\.aspx</LookFor>
        
<SendTo>~/ShowBlogContent.aspx?year=$1&amp;month=$2&amp;day=$3</SendTo>
      
</RewriterRule>
      
<RewriterRule>
        
<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
        
<SendTo><![CDATA[~/ShowBlogContent.aspx?year=$1&month=$2]]></SendTo>
      
</RewriterRule>
      
<RewriterRule>
        
<LookFor>~/(\d{4})\.aspx</LookFor>
        
<SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>
      
</RewriterRule>
    
</Rules>
  
</RewriterConfig>
<system.web>
    
<httpModules>
      
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
    
</httpModules>
</system.web>
</configuration>


这些重写规则表明了正则表达式的功能。在第一个规则中,我们使用模式 (\d{4})/(\d{2})/(\d{2})\.aspx 查找 URL。在简明英语中,它对应了这样一个字符串:首先是四个数字,后跟一个斜杠,然后是两个数字,后跟一个斜杠,然后再跟两个数字,最后是一个 .aspx。每个数字组周围的括号非常重要,通过它可以在相应的 <SendTo> 属性中引用这些括号内的匹配字符。 特别是,我们可以针对第一、第二和第三个括号组分别使用 $1、$2 和 $3 引用回括号内的匹配组。

注意:由于 Web.config 文件采用 XML 格式,但是必须对元素文字部分中的字符(如 &、< 和 >)进行转义。在第一个规则的 <SendTo> 元素中,& 被转义为 &amp;。在第二个规则的 <SendTo> 中使用了另外一种技术(使用 <![CDATA[...]]> 元素),无需对内部的内容进行转义。可以使用两种方法中的任何一种,并且都会得到相同的结果。
将URLRewriter.dll文件添加到Bin文件夹下面就可以实现了
网址重定向的Bin文件

posted on 2007-04-06 10:18  ainy360  阅读(391)  评论(0)    收藏  举报

导航