ASP.NET中URL重新導向的實現

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   使用   sp   for   檔案   

在Web.config檔案中添加規則:

<configSections></configSections>//=============下面是添加的代碼===============<RewriterConfig>   <Rules>      <!-- Blog 內容顯示程式規則 -->      <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></RewriterConfig>//=============上面是添加的代碼===============<system.web></system.web>

這些重寫規則表明了Regex的功能。在第一個規則中,我們使用模式 (\d{4})/(\d{2})/(\d{2})\.aspx 尋找 URL。在簡明英語中,它對應了這樣一個字串:首先是四個數字,後跟一個斜杠,然後是兩個數字,後跟一個斜杠,然後再跟兩個數字,最後是一個 .aspx。每個數字組周圍的括弧非常重要,通過它可以在相應的 <SendTo> 屬性中引用這些括弧內的匹配字元。 特別是,我們可以針對第一、第二和第三個括弧組分別使用 $1、$2 和 $3 引用回括弧內的匹配組。

注意:由於 Web.config 檔案採用 XML 格式,但是必須對元素文字部分中的字元(如 &、< 和 >)進行轉義。在第一個規則的 <SendTo> 元素中,& 被轉義為 &amp;。在第二個規則的 <SendTo> 中使用了另外一種技術(使用 <![CDATA[...]]> 元素),無需對內部的內容進行轉義。可以使用兩種方法中的任何一種,並且都會得到相同的結果。

在工程中引用Dll檔案URLRewriter.dll。
:http://files.cnblogs.com/ainy360/URLRewriter.rar

ASP.NET中URL重新導向的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.