使用URLRewriter实现URL重写
?
優(yōu)點
??? 1)隱藏真實URL,提高安全性
??? 2)更加友好的URL,好記(看博客園就行知道啦)
??? 3)便于搜素引擎收錄
??? .........
可能的缺點
????使用URL重寫可能導(dǎo)致:
??? 1)圖片路徑的問題
??? 2)CSS路徑的問題
??? 3)性能問題
??? .........
先下載DLL包,附帶了源碼的.
?
download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
?
URLRewriter.dll和ActionlessForm.dll
或者只使用DLL 本人已經(jīng)上傳:http://files.cnblogs.com/yun_shuai/URLRewriter.rar?含有上面2個DLL
?
使用方法? :WEB項目中添加引用
?
webconfig中配置:
1 <configSections> 節(jié)點中增加:
?? <section?name="RewriterConfig"?type="URLRewriter.Config.RewriterConfigSerializerSectionHandler,URLRewriter"/>
?
2 <configuration>根節(jié)點中增加URL重寫的配置:? 可以使用正則表達(dá)式
???
? <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 其他問題:
?
?? 回發(fā)后真實路徑再現(xiàn):
????? 1、將ActionlessForm.dll添加到項目的bin文件夾
????? 2、在aspx網(wǎng)頁的代碼中加<%@ Register TagPrefix="RW" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
????? 3、然后將form標(biāo)簽改為RW:Form(注意開始和結(jié)束都要改),TagPrefix可以自定義
另外:DOTNET3.5 提供了 System.Web.Routing?命名空間
可以自己實現(xiàn)想要的URL重寫.
?
參考文章:
http://www.cnblogs.com/Ferry/archive/2009/06/19/1506770.html
http://www.blogjava.net/Unmi/archive/2010/06/14/323349.html
從以上2篇文章地址可以看出,其服務(wù)器也使用了URL重寫方式
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/yun_shuai/archive/2010/07/31/1789314.html
總結(jié)
以上是生活随笔為你收集整理的使用URLRewriter实现URL重写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 游戏组件——挑战:创建NextBlock
- 下一篇: Windows XP和Windows 7
