Asp.net建立偽靜態頁面

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   檔案   2014   

下面是我研究了好幾天和同事一起才研究出來的,原創。

1偽靜態定義:

  偽靜態是相對真實靜態來講的,通常我們為了增強搜尋引擎的友好面,都將文章內容產生靜態頁面,但是有的朋友為了即時的顯示一些資訊。或者還想運用動態指令碼解決一些問題。不能用靜態方式來展示網站內容。但是這就損失了對搜尋引擎的友好面。怎麼樣在兩者之間找個中間方法呢,這就產生了偽靜態技術。就是展示出來的是以html一類的靜態頁面形式,但其實是用ASP一類的動態指令碼來處理的。

2偽靜態實現:

  2.1建立URL重寫類URLRewiter.cs 代碼如下:

     

 1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls;10 namespace WebApplication111 {12     public class URLRewiter : IHttpHandler13     {14         public URLRewiter()15         {16         // TODO: 在此處添加建構函式邏輯17           }18         public void ProcessRequest(HttpContext Context)19         {20             try21             {22                 //取得原始URL屏蔽掉參數23                 string Url = Context.Request.RawUrl;24                 //建立Regex25                 System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex26                 (@"/show-(\d+)\..+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);27                 //用Regex進行匹配28                 System.Text.RegularExpressions.Match m = Reg.Match(Url, Url.LastIndexOf("/"));//從最後一個“/”開始匹配29                 if (m.Success)//匹配成功30                 {31                     String RealPath = @"~/aspx/show.aspx?type=" + m.Groups[1];32                     Context.Server.Execute(RealPath);33                 }34                 else35                 {36                     Context.Response.Redirect(Context.Request.Url.ToString());37                 }38             }39             catch40             {41                 Context.Response.Redirect(Context.Request.Url.ToString());42             }43         }44 45         /// <summary>46         /// 實現“IHttpHandler”介面所必須的成員47         /// </summary>48         /// <value></value>49         /// Author:yoyo50         /// blog:http://yangmingsheng.cn51         public bool IsReusable52         {53             get { return false; }54         }55     }56 }
View Code

  2.2Web.Config的修改代碼如下:

   

1 <httpHandlers>2         <add verb="*" path="*/show-?*.aspx" type="WebApplication1.URLRewiter" />3         <add verb="*" path="*/show-?*.html" type="WebApplication1.URLRewiter" />4 </httpHandlers>

目前為止在本地瀏覽已經沒有問題:

   http://localhost:56321/aspx/show.aspx----可以改為下面的地址效果一樣,代表本地已經成功。

   http://localhost:56321/aspx/show-9.html

 3接下來是IIS的配置。(我的是win7  32位系統   IIS7)

   3.1將你的程式發布到iis與平時發布一樣,看看能否訪問。

 

 

   

  3.2選擇你的應用程式,雙擊中間的【處理常式映像】,進去後點擊右邊【添加指令碼映射】如:

    請求路徑:*.html

    可執行檔:C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll(注意64位系統進Framework64檔案夾)

    名稱隨意:

    

3.3再點擊右邊的【添加萬用字元指令碼映射】

   請求路徑不管,可執行檔同【處理常式映像】一樣,名稱也隨意。

4.目前為止已經配置成功,注意上面代碼配置的格式是show-9.html,就是減號後面只有一個整數,這個可以修改規則。

謝謝!

   

 

聯繫我們

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