UrlRewriter.Net重写

 


1.
下载 UrlRewriter.Net,请点这里

http://sourceforge.net/projects/urlrewriter/files/
2.
解压该文件;

其中自带reader.txt内容

UrlRewriter - a rule-based URL Rewriter for .NET.

Copyright (c)2007 Seth Yates

Author Seth Yates

Version 2.0 RC 1 build 6

 

Installation

============

1. Open your web project, or create a new one.

2. Add a reference to the Intelligencia.UrlRewriter assembly.

3. Open the web.config file.

4. Add Configuration section handler:

 

      <configSections>

              <section

                     name="rewriter"

                     requirePermission="false"

                     type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

       </configSections>

 

       This enables the URL Rewriter to read its configuration from the rewriteRules node in the

       web.config file.

 

5. Add UrlRewriter mapper HttpModule:

 

       <system.web>

              <httpModules>

                     <add

                            type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"

                            name="UrlRewriter" />

              </httpModules>

       </system.web>

      

       This enables the URL Rewriter to intercept web requests and rewrite URL requests.

 

6. Add some rules to your web.config file:

 

       <rewriter>

              <if url="/tags/(.+)" rewrite="/tagcloud.aspx?tag=$1" />

              <!-- same thing as <rewrite url="/tags/(.+)" to="/tagcloud.aspx?tag=$1" /> -->

       </rewriter>

 

       The syntax of the rewriter section is very powerful.  Refer to the help file for more details

       of what is possible.  The above rule assumes you have mapped all requests to the .NET runtime.

       For more information on how to do this, see http://urlrewriter.net/index.php/using/installation/

 

7. Compile and test!


3.
在您的网站中添加对Bin/Realease文件夹下Intelligencia.UrlRewriter.dll的引用:

vs解决方案中,右键点击您的网站,选择添加引用,找到我们刚才解压出来的   Intelligencia.UrlRewriter.dll文件,添加引用已经ok了;
4.
剩下的工作就是配置web.config文件了,这也是我存在疑惑的地方。
 
1 注意:一定加在web.config , <configSections>节的一级目录


           <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler,
                          Intelligencia.UrlRewriter" />

 

 
     
这个配置的作用是:使UrlRewriter.Net可以读取web.config文件中的rewrite配置节(这是我们后面要增加的配置信息)
      name
属性:指定与 type 属性中指定的配置节处理程序关联的配置节或元素的名称。这是该元素在配置文件的节设置区域中使用的名称。
      type
属性:指定用来执行如下操作的配置节处理程序类的名称:处理在 name 属性中指定的节或元素中的配置设置。这里有两个参数,第一个为完全类名,第二个就是程序集文件名;
2
      
        <system.web>
             <httpModules>
               <add  type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"  name="UrlRewriter" />
            </httpModules>
       </system.web>
   
添加到配置中的,它的作用是对web请求的url进行拦截,和对url的重写
     type
属性:第一个参数是程序集名称,第二个事处理请求的类名称;
     name
属性:对此http模块引用的名称。
3
    <configuration>
          ……
       <rewriter>
            <rewrite url="^.*-d([0-9]+)/?$" to="~/Catalog.aspx?DepartmentID=$1" processing="stop" />
            <rewrite url="^.*-d([0-9]+)/page-([0-9]+)/?$" to="~/Catalog.aspx?DepartmentID=$1&amp;Page=$2" processing="stop" />
      </rewriter>
            ……
    </configuration>
   
,它就是您想要实现的功能,您的url重写
   url
:这里是自定义的名称
   to:
这里是目标文件,也就是您要重写到的地方
   processing:
如果这条规则符合了,那么不再去寻找下一条匹配的规则,比如这里如果您的请求符合第一个,那么就不会再去匹配第二个了。
到这里一般就可以了,如果有必要再配置<httpHandlers>节点,但是如果你是跑aspx的话,相信iis里面已经配置好一切了。现在您可以测试了!

 

posted @ 2010-02-24 16:13  tangself  阅读(822)  评论(1编辑  收藏  举报