首先在以下地址:
download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
下載 MS 的 URLRewriter.dll,放到你的web程式的bin下。
註:以上地址下載的是微軟的一個完整的 URLrewrite 技術樣本。下載後是一個 MSDNURLRewriting.msi 檔案,安裝在本地機上,安裝後,在安裝目錄內有三個檔案夾,分別是:ActionlessForm ,RewriterTester,URLRewriter 這三個目錄。 其中 URLRewriter檔案夾便是一個完整的 URLRewrite 的項目樣本。此項目中的 BIN 目錄中有兩 個 dll,分別為
ActionlessForm.dll 和 URLRewriter.dll ,這兩個 dll 就是項目 ActionlessForm 和 URLRewriter 產生的 dll 類庫,是樣本項目 RewriterTester 實現 URLRwrite 技術所用到的類庫檔案。
如何把此技術應用到你自己的項目中去,其實很簡單:
首先,把 ActionlessForm.dll 和 URLRewriter.dll 兩個 dll 檔案放到你自己項目中的 bin 目錄下。
然後,修改你的 web.config 檔案,完整的 web.config 檔案如下:
(只需在普通的 web.config 檔案中填加兩個地方)
----------------------------------------------
1、
在 </configSections> 標籤上面填加:
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
2、
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
--------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!--
注意: 除了手動編輯此檔案以外,您還可以使用
Web 管理工具來配置應用程式的設定。可以使用 Visual Studio 中的
“網站”->“Asp.Net 配置”選項。
設定和注釋的完整列表在
machine.config.comments 中,該檔案通常位於
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<configSections>
<!-- The <configSections> element must contain a <section> tag for the <RewriterConfig> section element.
The type of the section handler is RewriterConfigSerializerSectionHandler, which is responsible for
deserializing the <RewriterConfig> section element into a RewriterConfig instance-->
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"
/>
</configSections>
<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>
<system.web>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"
/>
</httpModules>
<!--<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter"
/>
</httpHandlers>-->
<!--
設定 compilation debug="true" 將偵錯符號插入
已編譯的頁面中。但由於這會
影響效能,因此只在開發過程中將此值
設定為 true。
-->
<compilation debug="false"
/>
<!--
通過 <authentication> 節可以配置 ASP.NET 使用的
安全身分識別驗證模式,
以標識傳入的使用者。
-->
<authentication mode="Windows"
/>
<!--
如果在執行請求的過程中出現未處理的錯誤,
則通過 <customErrors> 節可以配置相應的處理步驟。具體說來,
開發人員通過該節可以配置
要顯示的 html 錯誤頁
以代替錯誤堆疊追蹤。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm"
/>
<error statusCode="404" redirect="FileNotFound.htm"
/>
</customErrors>
-->
</system.web>
</configuration>
然後,你的 aspx 程式就會按照你 web.config 檔案中的 Regex,轉換url 請求地址,實現 urlrewrit 技術。
比如:
1、http://localhost:4789/GuanTestURLRewrit/2003/07/18.aspx
按照 web.config 檔案中的正則,此 url 地址為被 重寫到以下真實存在的地址
http://localhost:4789/GuanTestURLRewrit/ShowBlogContent.aspx?year=2003&month=7&day=18
2、http://localhost:4789/GuanTestURLRewrit/2003/default.aspx
按照 web.config 檔案中的正則,此 url 地址為被 重寫到以下真實存在的地址
http://localhost:4789/GuanTestURLRewrit/ShowBlogContent.aspx?year=2003
3、http://localhost:4789/GuanTestURLRewrit/Products/Confections.aspx
按照 web.config 檔案中的正則,此 url 地址為被 重寫到以下真實存在的地址
http://localhost:4789/GuanTestURLRewrit/ListProductsByCategory.aspx?CategoryID=3
可以自己定義自己的Regex實現不同的 url 重寫規則
如果您想把 aspx 重寫成 html 後輟名,那麼則需要改動一下你的 web.config 檔案,
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
這樣好像還不行,那是因為在IIS裡面無法解析.html尾碼名(具體我也不知道怎麼叫...)
然後這樣操作:
右鍵點我的電腦-->管理-->展開'服務和應用程式'-->internet資訊服務-->找到你共用的目錄-->右鍵點擊屬性 -->點擊'配置'-->
映射下面 -->找到.aspx的可執行檔路徑 複製路徑-->粘貼路徑-->副檔名為".html"-->然後把檢查檔案是否存在的勾去掉 這樣就可以了
引用 ActionlessForm.DLL 檔案,是因為當頁面中有Post資料(如Post文本)。那麼這時重寫後的URL就會變為:[url=http://localhost/Test/2004/12]http://localhost/Test/2004/12/News.aspx?ID=12[/url] 真實的 url,露出原始的地址了,這顯然是不完善的,
附:為什麼URL就會變為:[url=http://localhost/Test/2004/12]http://localhost/Test/2004/12/News.aspx[/url]?ID=12
其實很簡單,因為在web.config中有這樣的一句:
<SendTo>~/Default.aspx?ID=$2</SendTo>
在沒有替換form之前,你查看頁面的源碼就可以看到,你的form的Action是到(以上面的例子):Default.aspx?ID=12
即.aspx頁面最後產生的HTML是:
<form id="Form1" name="Form1" method="post" action="Default.aspx?ID=12"></form>
解決方述問題方法:
首先把ActionlessForm.dll拷入你的項目中的bin目錄,然後在你的VS.net的項目中引用這個dll。再在你原有的(即沒重寫的).aspx頁面中
第一步:把這句加於代碼頂部:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
第二步:
<form id="Form1" method="post" runat="server">和</form>
替換成:
<skm:Form id="Form1" method="post" runat="server">和</skm:Form>
這樣,當此頁面有回傳資料時,則不會跳到真實的 url 上去。
還有,如果想用URL重寫後的格式為以目錄形式即不用加Default.aspx:
http://localhost/Test/2004/12
則要建立相應的目錄和檔案Default.aspx。
如上例子:http://localhost/Test/2004/12
則要建立2004目錄和在此目錄下建立12目錄,再在12目錄下建立Default.aspx檔案。檔案內容可為空白。
至於為什麼,是因為IIS如沒有找到目錄或檔案時會報錯。
另外需要注意的是,要在項目中添加 2003,2004 還有月份 01,02.03 .....12 這樣的目錄,然後在第一個目錄下添加 default.aspx ,內容只為一句話:<%@ Page %> 即可,防止使用者輸入目錄斷行符號後,找不到檔案,出錯。