zoukankan      html  css  js  c++  java
  • 利用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>中第二 第三个()中的参数。

  • 相关阅读:
    mysql 在orderby和limit混合使用时重复数据问题
    springboot启动类 注解
    redis RDB和AOF两种持久化的区别
    C#解析逻辑字符串【x>y&&a>b||p=r】
    删除例如联想笔记本系统隐藏分区
    通过贝叶斯算法实现自动识别类别
    将可执行exe文件注册成windows服务
    Windows10中的IIS10安装php manager和IIS URL Rewrite 2.0组件的方法
    添加钩子监听全局鼠标或键盘事件
    C# DateTime.Now和DateTime.UtcNow的区别
  • 原文地址:https://www.cnblogs.com/ac1985482/p/B20090725.html
Copyright © 2011-2022 走看看