采用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&month=3&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&month=3&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&month=$3&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&month=$3&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>