• asp.net UrlRewrite 技术的实现



    首先在以下地址:

    download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

     下载 MS 的 URLRewriter.dll,放到你的web程序的bin下。

    注:以上地址下载的是微软的一个完整的 URLrewrite 技术示例。下载后是一个 MSDNURLRewriting.msi 文件,安装在本地机上,安装后,在安装目录内有三个文件夹,分别是:ActionlessForm ,RewriterTester,URLRewriter 这三个目录。 其中 URLRewriter 文件夹便是一个完整的 URLRewrite 的项目示例。此项目中的 BIN 目录中有两 个 dll,分别为
    ActionlessForm.dllURLRewriter.dll ,这两个 dll 就是项目 ActionlessForm  和 URLRewriter 产生的 dll 类库,是示例项目 RewriterTester 实现 URLRwrite 技术所用到的类库文件。

    如何把此技术应用到你自己的项目中去,其实很简单:

    首先,把 ActionlessForm.dllURLRewriter.dll 两个 dll 文件放到你自己项目中的 bin 目录下。
    然后,修改你的 web.config 文件,完整的 web.config 文件如下:

    (只需在普通的 web.config 文件中填加两个地方)
    ----------------------------------------------
    1、

    在  </configSections> 标签上面填加:
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

    2、

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


    --------------------------------------------
    <?xml version="1.0" encoding="utf-8"?>
    <!-- 
        注意: 除了手动编辑此文件以外,您还可以使用 
        Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
         “网站”
    ->“Asp.Net 配置”选项。
        设置和注释的完整列表在 
        machine.config.comments 中,该文件通常位于 
        \Windows\Microsoft.Net\Framework\v2.x\Config 中
    -->
    <configuration>

      
    <configSections>
        
    <!-- The <configSections> element must contain a <section> tag for the <RewriterConfig> section element.
                The type of the section handler 
    is RewriterConfigSerializerSectionHandler, which is responsible for
                deserializing the 
    <RewriterConfig> section element into a RewriterConfig instance -->
        
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
      
    </configSections>

      
    <RewriterConfig>
        
    <Rules>
          
    <!-- Rules for Blog Content Displayer -->
          
    <RewriterRule>
            
    <LookFor>~/(\d{4})/(\d{2})/(\d{2})\.aspx</LookFor>
            
    <SendTo>~/ShowBlogContent.aspx?year=$1&amp;month=$2&amp;day=$3</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
            
    <SendTo><![CDATA[~/ShowBlogContent.aspx?year=$1&month=$2]]></SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/(\d{4})/Default\.aspx</LookFor>
            
    <SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>
          
    </RewriterRule>


          
    <!-- Rules for Product Lister -->
          
    <RewriterRule>
            
    <LookFor>~/Products/Default\.aspx</LookFor>
            
    <SendTo>~/ListCategories.aspx</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/Beverages\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=1</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/Condiments\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=2</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/Confections\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=3</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/Dairy\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=4</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/GrainsCereals\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=5</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/MeatPoultry\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=6</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/Produce\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=7</SendTo>
          
    </RewriterRule>
          
    <RewriterRule>
            
    <LookFor>~/Products/Seafood\.aspx</LookFor>
            
    <SendTo>~/ListProductsByCategory.aspx?CategoryID=8</SendTo>
          
    </RewriterRule>
        
    </Rules>
      
    </RewriterConfig>
      
      
        
    <system.web>

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

          
    <!--<httpHandlers>
            
    <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
        
    </httpHandlers>-->
          
            
    <!-- 
                设置 compilation debug
    ="true" 将调试符号插入
                已编译的页面中。但由于这会 
                影响性能,因此只在开发过程中将此值 
                设置为 
    true
            
    -->
            
    <compilation debug="false" />
            
    <!--
                通过 
    <authentication> 节可以配置 ASP.NET 使用的 
                安全身份验证模式,
                以标识传入的用户。 
            
    -->
            
    <authentication mode="Windows" />
            
    <!--
                如果在执行请求的过程中出现未处理的错误,
                则通过 
    <customErrors> 节可以配置相应的处理步骤。具体说来,
                开发人员通过该节可以配置
                要显示的 html 错误页
                以代替错误堆栈跟踪。

            
    <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
                
    <error statusCode="403" redirect="NoAccess.htm" />
                
    <error statusCode="404" redirect="FileNotFound.htm" />
            
    </customErrors>
            
    -->
        
    </system.web>
    </configuration>

    然后,你的 aspx 程序就会按照你 web.config 文件中的 正则表达式,转换url 请求地址,实现 urlrewrit 技术。
    比如:

    1、http://localhost:4789/GuanTestURLRewrit/2003/07/18.aspx
    按照 web.config 文件中的正则,此 url 地址为被 重写到以下真实存在的地址
    http://localhost:4789/GuanTestURLRewrit/ShowBlogContent.aspx?year=2003&month=7&day=18

    2、http://localhost:4789/GuanTestURLRewrit/2003/default.aspx
    按照 web.config 文件中的正则,此 url 地址为被 重写到以下真实存在的地址
    http://localhost:4789/GuanTestURLRewrit/ShowBlogContent.aspx?year=2003

    3、http://localhost:4789/GuanTestURLRewrit/Products/Confections.aspx
    按照 web.config 文件中的正则,此 url 地址为被 重写到以下真实存在的地址
    http://localhost:4789/GuanTestURLRewrit/ListProductsByCategory.aspx?CategoryID=3


    可以自己定义自己的正则表达式实现不同的 url 重写规则

    如果您想把 aspx 重写成 html 后辍名,那么则需要改动一下你的 web.config 文件,
    <httpHandlers>
            <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
         <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
    </httpHandlers>
    这样好像还不行,那是因为在IIS里面无法解析.html后缀名(具体我也不知道怎么叫...)
     然后这样操作:
                 右键点我的电脑-->管理-->展开'服务和应用程序'-->internet信息服务-->找到你共享的目录-->右键点击属性 -->点击'配置'-->
    映射下面 -->找到.aspx的可执行文件路径 复制路径-->粘贴路径-->扩展名为".html"-->然后把检查文件是否存在的勾去掉 这样就可以了

    引用 ActionlessForm.dll 文件,是因为当页面中有Post数据(如Post文本)。那么这时重写后的URL就会变为:http://localhost/Test/2004/12/News.aspx?ID=12 真实的 url,露出原始的地址了,这显然是不完善的,

    附:为什么URL就会变为:http://localhost/Test/2004/12/News.aspx?ID=12
    其实很简单,因为在web.config中有这样的一句:
    <SendTo>~/Default.aspx?ID=$2</SendTo>
    在没有替换form之前,你查看页面的源码就可以看到,你的form的Action是到(以上面的例子):Default.aspx?ID=12
    即.aspx页面最后生成的HTML是:
    <form id="Form1" name="Form1" method="post" action="Default.aspx?ID=12"></form>

    解决方述问题方法:

    首先把ActionlessForm.dll拷入你的项目中的bin目录,然后在你的VS.net的项目中引用这个dll。再在你原有的(即没重写的).aspx页面中

    第一步:把这句加于代码顶部:
    <%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
    第二步:
    <form id="Form1" method="post" runat="server">和</form>
    替换成:
    <skm:Form id="Form1" method="post" runat="server">和</skm:Form>

    这样,当此页面有回发数据时,则不会跳到真实的 url 上去。


    还有,如果想用URL重写后的格式为以目录形式即不用加Default.aspx:
    http://localhost/Test/2004/12
    则要新建相应的目录和文件Default.aspx。
    如上例子:http://localhost/Test/2004/12
    则要新建2004目录和在此目录下新建12目录,再在12目录下新建Default.aspx文件。文件内容可为空。

    至于为什么,是因为IIS如没有找到目录或文件时会报错。





    另外需要注意的是,要在项目中添加 2003,2004 还有月份 01,02.03 .....12 这样的目录,然后在第一个目录下添加 default.aspx ,内容只为一句话:<%@ Page %> 即可,防止用户输入目录回车后,找不到文件,出错。



  • 相关阅读:
    ASP.NET学习线路(转)
    创建variant二维数组
    域名”A记录,MX记录,CNAME记录,TTL值,URL转发”解释
    数据库表行转列,列转行终极方案(转)
    [转载]oracle备份与恢复精华资料
    事务的概念
    Velocity语言的介绍
    初步认识JUnit
    JSON基本用法
    matplotlib入门
  • 原文地址:https://www.cnblogs.com/tiger8000/p/945901.html
Copyright © 2020-2023  润新知