url重写或伪静态的优点
1)隐藏真实URL,提高安全性
2)更加友好的URL,好记
3)便于搜素引擎收录
可能的缺点
使用URL重写可能导致:
1)图片路径的问题
2)CSS路径的问题
3)性能问题
先下载DLL包,附带了源码的.
http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
URLRewriter.dll和ActionlessForm.dll
或只使用DLL,下载地址:
1、URL重写urlrewriter.dll组件下载地址(附完整示例代码)
2、微软URL重写组件urlrewriter.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&month=$2&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重写。