ASP.NET实现URL重写(伪静态)

1、在项目中引入程序集URLRewriter,即在网站的Bin文件夹中添加URLRewriter.dll。

点此下载URLRewriter.dll

 

2、在web.config中的configuration节点下添加URLRewriter的配置。

<configSections>

    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>

</configSections>

注意:configSections必须是configuration节点下的第一个子元素。

 

3、在web.config中的system.web节点下添加URLRewriter的配置。

<httpModules>

    <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>

</httpModules>

 

4、在web.config中的configuration节点下配置url重写的规则。

<RewriterConfig>

    <Rules>

        <RewriterRule>

            <LookFor>~/article/(\d+)/</LookFor>

            <SendTo>~/article_info.aspx?articleId=$1</SendTo>

        </RewriterRule>

    </Rules>

</RewriterConfig>

url重写规则说明:

(1)、<RewriterRule>可出现多次,每个重写规则都需要添加一个<RewriterRule>节点。

(2)、在获得匹配<LookFor>节点的url后,转向到相应的<SendTo>节点的url。

(3)、<LookFor>中可使用正则表达式来表示参数。路径转向后,<SendTo>节点中的占位符$1将会用<LookFor>节点中正则表达式所匹配的参数来替换。

(4)、<SendTo>配置中的url占位符可出现多个,如:$2、$3,占位符将按顺序依次由<LookFor>节点中则表达式所匹配的参数来替换。

来源: 谢斌个人博客ASP.NET实现URL重写(伪静态)
转载请以链接形式标明本文地址!本文地址:https://www.xb02.com/article/90
发表评论

发表评论