在ASP.Net2.0中使用UrlRewritingNet實現連結重寫

Hellen.Li發表於2013-08-25

採用UrlRewritingNet.UrlRewriter.dll來輕鬆實現
UrlRewritingNet.UrlRewriter.dll 可從其官方網站下載:
http://www.urlrewriting.net


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>

 

  <system.webServer>

<modules>
          <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/> <!--lzx add-->
    </modules>

  <system.webServer>


3 新增連結重寫的設定  <configuration> 節點下任一地方
<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>

 <system.webServer>

<modules>

          <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/> <!--lzx add-->     </modules>

  <system.webServer>


</configuration>

相關文章