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)定義多個正則表達式規則,匹配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:IHttpHandler 12 { 13 /// <summary> 14 /// 自定義 HttpHandler 15 /// </summary> 16 /// <param name="context"></param> 17 public void ProcessRequest(HttpContext context) 18 { 19 try 20 { 21 string url = context.Request.RawUrl;//獲取用戶請求的URL地址信息 22 Regex Reg = new Regex(@"/detail-(\d+)-(\d+)\..+", RegexOptions.IgnoreCase);//建立正則表達式 23 Match m = Reg.Match(url, url.LastIndexOf("/"));//用正則表達式進行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 else 30 { 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 IsReusable 45 { 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&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.相關參考資料
(4)ASP.NET偽靜態頁面的實現和偽靜態在IIS7.0中的配置