ASP.NET偽靜態實現

來源:互聯網
上載者:User

1.什麼是偽靜態?使用偽靜態作用是什嗎?

定義:動態網頁通過重寫URL的方法實現去掉動態網頁的參數,但在實際的網頁目錄中並沒有必要實現存在重寫的頁面。

例如:我們當訪問地址http://www.cnblogs.com/ForEvErNoME/archive/2012/06/05/2529259.html時,你會認為在站台伺服器下存在名為2529259.html檔案,其實實際上它可能是不存在的,而可能你看到的內容是通過重新導向/archive/article.aspx?year=2012&month=06&day=05&id=2529259顯示出來的。

為什麼要這樣做呢?

(1)增強URL的友好性,方便使用者記憶URL。

(2)提高搜尋引擎抓取,很多搜尋引擎更看好靜態HTML頁。

(3)加強安全性,因為隱藏了參數"year"、"month"、"day"、"id",使網站沒有那麼容易受到攻擊。

 

2.偽靜態實現的基本思路

(1)自訂HttpHandler類,實現IHttpHandler介面

(2)擷取使用者請求的URL地址資訊

(3)定義多個Regex規則,匹配URL字串

(4)重新導向真實的URL地址資訊

 

3.自訂UrlRewriter類

原理:通過實現介面IHttpHandler來接管HTTP請求。

(1)定義UrlRewriter.cs類

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Text; 6 using System.Text.RegularExpressions; 7  8 /// <summary> 9 ///UrlRewriter 偽靜態Url重寫10 /// </summary>11 public class UrlRewriter:IHttpHandler12 {13     /// <summary>14     /// 自訂 HttpHandler15     /// </summary>16     /// <param name="context"></param>17     public void ProcessRequest(HttpContext context)18     {19         try20         {21             string url = context.Request.RawUrl;//擷取使用者請求的URL地址資訊22             Regex Reg = new Regex(@"/detail-(\d+)-(\d+)\..+", RegexOptions.IgnoreCase);//建立Regex 23             Match m = Reg.Match(url, url.LastIndexOf("/"));//用Regex進行URL字串24             if (m.Success)//匹配成功25             {26                 string RealPath = @"~/admin/detail.aspx?type=" + m.Groups[1] + "&id=" + m.Groups[2];//重新導向真實的地址資訊27                 context.Server.Execute(RealPath);28             }29             else30             {31                 context.Response.Redirect(context.Request.Url.ToString());32             }33 34         }35         catch (Exception ex)36         { 37             context.Response.Redirect(context.Request.Url.ToString());  38         }39     }40 41     /// <summary>  42     ///  如果 System.Web.IHttpHandler 執行個體可再次使用,則為 true;否則為 false。43     /// </summary>  44     public bool IsReusable45     {46         get { return false; }47     }48 }

 (2)在web.config中<system.web>節點下添加配置資訊

<?xml version="1.0"?><configuration><system.web>  <httpHandlers>    <!--使用自訂UrlRewriter類-->    <add verb="*" path="*/detail-?*-?*.html" type="UrlRewriter"/>    </httpHandlers>    <compilation debug="true" targetFramework="4.0"/>  </system.web></configuration>

解釋:

verb屬性:是指處理常式支援的HTTP動作。如果某個處理常式支援所有的HTTP動作,請使用"*",否則使用逗號分隔的列表列出支援的動作。因此如果你的處理常式只支援HTTP "GET"和"POST",那麼verb屬性就應該是"GET", "POST"。

path屬性:指定了需要調用處理常式的路徑和檔案名稱(可以包含萬用字元)。例如,如果你希望自己的處理常式只有在test.html檔案被請求的時候才被調用,那麼path屬性就包含”test.html“,如果你希望含有.html尾碼的所有檔案都調用處理常式,path屬性應該為"*.html"。

type屬性:是指綁定的類名以及包括命名空間(如果有的話)。ASP.NET運行時首先搜尋應用程式的bin目錄中的組件DLL,接著在全域組件緩衝(GAC)中搜尋。

(3)開啟網站輸入http://localhost:28053/偽靜態/detail-1-1.html,顯示的是”admin/detail.aspx?type=1&id=1”的內容。(執行個體下載在文章最後)

 

4.使用微軟的URLRewriter.dll

(1)添加URLRewriter.dll引用

(2)配置web.config基本資料

<?xml version="1.0"?><configuration>  <!--使用URLRewriter.dll --><configSections>  <section name="RewriterConfig" requirePermission="false" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /></configSections><RewriterConfig>  <Rules>    <RewriterRule>      <LookFor>~/detail/([0-9]*)/([0-9]*).html</LookFor>      <SendTo>~/admin/detail.aspx?type=$1&amp;id=$2</SendTo>    </RewriterRule>  </Rules></RewriterConfig><system.web>  <httpHandlers>    <!--使用URLRewriter.dll    -->  <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />  <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /></httpHandlers>    <compilation debug="true" targetFramework="4.0"/>  </system.web></configuration>

簡單介紹

<RewriterConfig>   <Rules>   <RewriterRule>      <LookFor>要尋找的模式</LookFor>      <SendTo>要用來替換模式的字串</SendTo>   </RewriterRule>   <RewriterRule>      <LookFor>要尋找的模式</LookFor>      <SendTo>要用來替換模式的字串</SendTo>   </RewriterRule>   </Rules></RewriterConfig>

用reflector查看URLRewriter.dll,節點的資訊是已經在類中定義好的,不能更改。

(3)同樣的,開啟網站輸入http://localhost:28053/偽靜態/detail-1-1.html,顯示的是”admin/detail.aspx?type=1&id=1”的內容。

 

5.相關參考資料

(1)在ASP.NET中執行URL重寫

(2)ASP.NET 實現偽靜態網頁方法

(3)IIS7 偽靜態 web.config 配置方法

(4)ASP.NET偽靜態頁面的實現和偽靜態在IIS7.0中的配置

 

 執行個體下載

 

作者: ForEvErNoME
出處: http://www.cnblogs.com/ForEvErNoME/
歡迎轉載或分享,但請務必聲明文章出處。如果文章對您有協助,希望你能 推薦 或 關注

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.