ASP.NET URLRewriter重写

时间:2023-03-09 01:01:02
ASP.NET URLRewriter重写

URLRewriter重写是微软官方出的第三方重写插件

下载地址:http://download.****.net/detail/ysn1314/5421587

下载后在项目中添加引用,然后再配置文件Web.config中配置,在<configuration></configuration>节点中插入如下代码:

  <configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections> <RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/(.*).html(.*)</LookFor>
<SendTo>~/$.aspx?$</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/index.html</LookFor>
<SendTo>~/index.aspx</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<LookFor>~/(.*).html(.*)</LookFor> 所表示的是在浏览器中所显示的地址
<SendTo>~/$1.aspx?$2</SendTo> 指向项目中真实的地址
例如上面这个 index.html 她会指向index.aspx
index.html?id=1 最终会指向index.aspx?id=1,<LookFor></LookFor>中的匹配规则由自己定

接下来的配置在集成模式与经典模式中是有所区别的,

集成模式要在<system.webServer>节点中添加代码:

<system.webServer>
  <handlers>
<add path="*.html" verb="*" name="URLRewriters" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
<add path="*.aspx" verb="*" name="URLRewriter" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
</handlers>
</system.webServer>

经典模式要在<system.web>中添加配置

<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules> <httpHandlers>
<add verb="*" path="/*/list/*" type="URLRewriter.RewriterFactoryHandler,URLRewriter" />
<add verb="*" path="/group/*" type="URLRewriter.RewriterFactoryHandler,URLRewriter" />
</httpHandlers>