UrlRewriter 是微軟封裝好了的一個URL重寫組件。使用它可以讓我節約很多自已開發的時間。
好了,開始講述我的應用經驗,這只是很菜鳥的經驗,高手就不用看了。
第一步,請從此下載此組件。解壓,把UrlRewriter.dll copy到你的項目 bin 目錄下。
第二步,在Web.config中加入:
<?xml version="1.0" encoding="gb2312" ?>
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
第二步,加入重寫的規則節點: 如:
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/Sell/(.[0-9]*)/.html</LookFor>
<SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Sell/Search_Sell/.aspx</LookFor>
<SendTo>~/Search/Search_Sell.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buy/(.[0-9]*)/.html</LookFor>
<SendTo>~/Search/Search_Buy.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buys/(.[0-9]*)/.html</LookFor>
<SendTo>~/Buys/Show.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
這個就要根據你的需要了,如果你對正則表達式不熟,那麼沒辦法,要麼憑藉你的高智商去找其中規律,稍稍改一下就能爲你所用了。呵呵。如果實在搞不 清,那就自己GOOGLE一下正則表達式吧。(本人開始是參考別人的配置猜的,竟然用對了,呵呵。後來還是看了一下相關資料,發現這東東很有用。)
第三步,加入模塊配置(寫在<system.web>裏面): 如:
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
(這裏表示使用HTTP程序來處理重寫)
好了,到了現在我們可以試一下看。
於是輸入:http://127.0.0.1:8080/Sell/1.aspx 出現了,呵呵。但是如果所它改爲:http://127.0.0.1:8080/Sell/1.html 暈,發現不行。汗。。。 呵呵,原因是沒把HTML的解析用 asp.net 的ISAPI來解析。 辦法是。。。
第四步,在IIS/你的站點/屬性/主目錄/配置/映謝 加入一個和 aspx 頁面的配置相同的擴展名項。注意“確認文件是否存在”不要勾選,否則會出現找不到文件。
現在再來試試看。什麼?#¥%#¥%#,還是不行。呵呵。不要急,咱們回過頭再來看看,原來在 web.config 中我們沒有配置 .html 也使用模塊此解析。
第五步,在模塊配置中加入:
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
現在總可以了吧,呵呵。終於看到了,興奮吧。不要急,這還只是最簡單的。如果你的頁面有回傳。比如說放了DATAGRID,有分頁的,你點到下一頁就發現,暈倒,又出問題了。 這下怎麼辦呢,這個其實微軟件的網站上就有說到,我在這裏簡述一下了。
第六步,加入窗體回傳保持的組件: 在原來你下載的項目裏找到 ActionlessForm.dll 放到你的項目 bin 目錄下。
然後在你的這個頁面中加入: <%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %> 再把你的<Form...>改爲: <skm:Form id="你的表單名" method="post" runat="server"> ..... </skm:Form>
That's All.現在你可以高枕無憂了。一切如你所願。
最後,恭祝各位一切順利。