利用URLRewriter重写url地址实现伪静态并配置web.config,配置不正确会出现URLRewriter 没有实现 IHttpModule

1. 下载URLRewriter(找不到资源的上csdn,我就是从csdn上下载的)


2. 安装后再bin目录下找到URLRewriter.dll文件


3. 在web.config文件中加入如下代码

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

4. 用于指定配置节”RewriterConfig”的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”该类存在于bin目录下的URLRewriter .dll文件中


5. 在web.config文件中的system.web节点下加入如下代码

<httpHandlers>
      <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
 </httpHandlers>

6. 重写URL通过在Web.config中增加节点实现加载新的config文件进行配置

<RewriterConfig configSource="URLRewriter.config"/>
新建URLRewriter.config文件

<?xml version="1.0"?>
<RewriterConfig>
  <Rules>
    <RewriterRule>
      <LookFor>~/admin/keyworddetail/(.+)\.aspx</LookFor>
      <SendTo>~/admin/keyworddetail.aspx?kwpkid=$1</SendTo>
    </RewriterRule>
    <RewriterRule>
      <LookFor>~/admin/testsubject/(.+)\.aspx</LookFor>
      <SendTo>~/admin/testsubject.aspx?tstpkid=$1</SendTo>
    </RewriterRule>
  </Rules>
</RewriterConfig>
不创建配置文件,直接放web.config中的节点下面 
<RewriterConfig>
  <Rules>
    <RewriterRule>
      <LookFor>~/admin/keyworddetail/(.+)\.aspx</LookFor>
      <SendTo>~/admin/keyworddetail.aspx?kwpkid=$1</SendTo>
    </RewriterRule>
    <RewriterRule>
      <LookFor>~/admin/testsubject/(.+)\.aspx</LookFor>
      <SendTo>~/admin/testsubject.aspx?tstpkid=$1</SendTo>
    </RewriterRule>
  </Rules>
</RewriterConfig>

注:如果是集成模式,需要稍作调整system.webServer下添加

    <modules>
        <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
    </modules>
	
    <handlers>
      <add name="Rewriter" path="*" verb="*" modules="IsapiModule" 
       scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" 
      resourceType="Unspecified" requireAccess="None"
       preCondition="classicMode,runtimeVersionv4.0,bitness32" />
    </handlers>



集成模式不按照如此配置会出现错误提示:URLRewriter 没有实现 IHttpModule

玩咖指针 2020-03-18 21:41:36