使用URLRewriter实现URL重写

优点

    1)隐藏真实URL,提高安全性

    2)更加友好的URL,好记(看博客园就行知道啦)

    3)便于搜素引擎收录

    .........

可能的缺点

    使用URL重写可能导致:

    1)图片路径的问题

    2)CSS路径的问题

    3)性能问题

    .........

先下载DLL包,附带了源码的.

 

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

 

URLRewriter.dll和ActionlessForm.dll

或者只使用DLL 本人已经上传:https://files.cnblogs.com/yun_shuai/URLRewriter.rar 含有上面2个DLL

 

使用方法  :WEB项目中添加引用

 

webconfig中配置:

1 <configSections> 节点中增加:

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

 

2 <configuration>根节点中增加URL重写的配置:  可以使用正则表达式

     <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>

 

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

  或者

<httpHandlers> 增加   <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

 

4 完成.

 

5 其他问题:

 

 

   回发后真实路径再现:

      1、将ActionlessForm.dll添加到项目的bin文件夹

      2、在aspx网页的代码中加<%@ Register TagPrefix="RW" Namespace="ActionlessForm" Assembly="ActionlessForm" %>

      3、然后将form标签改为RW:Form(注意开始和结束都要改),TagPrefix可以自定义

另外:DOTNET3.5 提供了 System.Web.Routing 命名空间

可以自己实现想要的URL重写.

 

参考文章:

http://www.cnblogs.com/Ferry/archive/2009/06/19/1506770.html

http://www.blogjava.net/Unmi/archive/2010/06/14/323349.html

从以上2篇文章地址可以看出,其服务器也使用了URL重写方式

posted @ 2014-11-06 14:48  bxzjzg  阅读(212)  评论(0编辑  收藏  举报