利用URLRewriter重写url

    首先,当然是下载URLRewriter了 download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi  下载安装后再bin目录下找到URLRewriter.dll文件

    然后把这个文件引用到项目中,下面开始配置

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

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

</configuration>

   其中

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

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

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

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

    这段代码的意思是:将文件扩展名为 .html 的文件的所有 HTTP 请求映射到类 URLRewriter.RewriterFactoryHandler 具体可以看MSDN,在这里我开始犯了个错误吧path=“*.html”写成了path=“*.aspx”导致了找不到页面,发生404的错误

   3 重写url

        和1一样 ,同样是放在<configuration></configuration>节点下面 

     关键就是

<RewriterConfig>
    
<Rules>
      
<RewriterRule>
        
<LookFor>~/Shownews/news(\d+)\.html</LookFor>
        
<SendTo>~/Shownews.aspx?ShowID=$1</SendTo>
      
</RewriterRule>
      
<RewriterRule>
        
<LookFor>~/product(\d+)\.html</LookFor>
        
<SendTo>~/Showproduct.aspx?ShowID=$1</SendTo>
      
</RewriterRule>
    
</Rules>
  
</RewriterConfig>

 其中关键在uml的转换 

<LookFor>~/Shownews/news(\d+)\.html</LookFor>

<SendTo>~/Shownews.aspx?ShowID=$1</SendTo>

      意思是把第一个路径转成另一个路径。其中<LookFor>()中的正则表达式就是第二句中的参数$1 .

同样也可以用$2 $3来表示<LookFor>中第二 第三个()中的参数。

原文地址:https://www.cnblogs.com/ac1985482/p/B20090725.html