如果你有在IE中查看當(dāng)前瀏覽頁(yè)面HTML源代碼的習(xí)慣,你也許常會(huì)看到類似以下的代碼片斷:
<input type="hidden" name="__VIEWSTATE" value="dDwtMzU5NzUyMTQ1O3Q8O2w8aTwwPjs+O2w8dDw7bDxpPDA+Oz47bDx0PDtsPG …… 聰明的你一定會(huì)問(wèn),這是什么?有什么作用?它與本篇文章有何轉(zhuǎn)折親關(guān)系?各位看官,且聽(tīng)我慢慢道來(lái)。 其實(shí),這就是MS在Asp.net應(yīng)用ViewState技術(shù)的特征表現(xiàn)。為了頁(yè)面能在PostBack后依然能讀取服務(wù)器控件原有的狀態(tài)數(shù)據(jù),Asp.net中使用了ViewState技術(shù),而ViewState技術(shù)本質(zhì)上是用一個(gè)默認(rèn)名稱為"__VIEWSTATE的Hidden類型表單域來(lái)保存和傳遞數(shù)據(jù)(這些數(shù)據(jù)是經(jīng)過(guò)了序列化后Base64編碼的字符串值,且是在方法Page.SavePageStateToPersistenceMedium輸出前保存、并由Page.LoadPageStateFromPersistenceMedium加載)。雖然我們可以通過(guò)三種級(jí)別來(lái)輕松禁用掉這些數(shù)據(jù)的往返傳遞: Machine級(jí) 在machine.config中設(shè)置<pages enableViewStateMac='false' />
Application級(jí) 在Web Applicatin的web.config中設(shè)置<pages enableViewStateMac='false' /> 單頁(yè)面級(jí) 在該頁(yè)面中設(shè)置<%@Page enableViewStateMac='false' %>或通過(guò)代碼設(shè)置Page.EnableViewStateMac = false; 可是,如果我們完全能通過(guò)禁用ViewState來(lái)解決數(shù)據(jù)傳輸負(fù)擔(dān)而且不產(chǎn)生副作用的話,那MS的架構(gòu)師們也不會(huì)傻到如此可愛(ài)的地步(可有可無(wú)的東東留它何用?),正因我們往往不能通過(guò)簡(jiǎn)單的禁用來(lái)解決這個(gè)傳輸負(fù)擔(dān)問(wèn)題,所以我們只能另辟路徑使之在網(wǎng)絡(luò)往返中傳輸量盡可能地小,于是,壓縮成了我們的首選。只要我們重載Page類的SavePageStateToPersistenceMedium()方法與LoadPageStateFromPersistenceMedium()方法,并在重載方法中對(duì)數(shù)據(jù)進(jìn)行壓縮與解壓的處理即可。開(kāi)源項(xiàng)目SharpZipLib提供的類GZipInputStream與GZipOutputStream進(jìn)入了我們的視野,為了方便,不妨寫(xiě)個(gè)類CompressionHelper,代碼如下: 1using System.IO;
2using ICSharpCode.SharpZipLib.GZip; 3 4namespace Ycweb.Components 5{ 6 /**//// <summary> 7 /// Summary description for CompressionHelper. 8 /// </summary> 9 public class CompressionHelper 10 { 11 public CompressionHelper() 12 { 13 // 14 // TODO: Add constructor logic here 15 // 16 } 17 18 /**//// <summary> 19 /// 壓縮數(shù)據(jù) 20 /// </summary> 21 /// <param name="data">待壓縮的字節(jié)數(shù)組</param> 22 /// <returns>壓縮后的字節(jié)數(shù)組</returns> 23 public static byte[] CompressByte(byte[] data) 24 { 25 MemoryStream ms = new MemoryStream(); 26 Stream s=new GZipOutputStream(ms); 27 s.Write( data, 0, data.Length ); 28 s.Close(); 29 return ms.ToArray(); 30 } 31 32 /**//// <summary> 33 /// 解壓數(shù)據(jù) 34 /// </summary> 35 /// <param name="data">待解壓的字節(jié)數(shù)組</param> 36 /// <returns>解壓出的字節(jié)數(shù)組</returns> 37 public static byte[] DeCompressByte(byte[] data) 38 { 39 byte[] writeData = new byte[2048]; 40 MemoryStream ms= new MemoryStream( data ); 41 Stream sm = new GZipInputStream(ms) as Stream; 42 MemoryStream outStream = new MemoryStream(); 43 while (true) 44 { 45 int size = sm.Read(writeData,0, writeData.Length ); 46 if (size >0) 47 { 48 outStream.Write(writeData,0,size); 49 } 50 else 51 { 52 break; 53 } 54 } 55 sm.Close(); 56 byte[] outArr = outStream.ToArray(); 57 outStream.Close(); 58 return outArr; 59 } 60 } 61} 然后我們?cè)谂缮陬怭age的頁(yè)面控制基類中重載方法LoadPageStateFromPersistenceMedium()與SavePageStateToPersistenceMedium(Object viewState),代碼如下: 1Load & Save ViewState Data#region Load & Save ViewState Data 2 protected override object LoadPageStateFromPersistenceMedium() 3 { 4//從自己注冊(cè)的隱藏域__SmartViewState中讀取數(shù)據(jù) 5 string viewState = Request.Form["__SmartViewState"]; 6 byte[] bytes = Convert.FromBase64String(viewState); 7 //調(diào)用上面提供的靜態(tài)方法CompressionHelper.DeCompressByte()來(lái)解壓 數(shù)據(jù)
8 bytes = CompressionHelper.DeCompressByte(bytes); 9 LosFormatter formatter = new LosFormatter(); 10 return formatter.Deserialize(Convert.ToBase64String(bytes)); 11 12 } 13 14 protected override void SavePageStateToPersistenceMedium(Object viewState) 15 { 16 LosFormatter formatter = new LosFormatter(); 17 StringWriter writer = new StringWriter(); 18 formatter.Serialize(writer, viewState); 19 string viewStateString = writer.ToString(); 20 byte[] bytes = Convert.FromBase64String(viewStateString); 21 //調(diào)用上面提供的靜態(tài)方法CompressionHelper.CompressByte()來(lái)壓縮數(shù)據(jù) 22 bytes = CompressionHelper.CompressByte(bytes); 23 24 //注冊(cè)一個(gè)新的隱藏域__SmartViewState,你也可以自己定義 25 this.RegisterHiddenField("__SmartViewState", Convert.ToBase64String(bytes)); 26 } 27#endregion 經(jīng)過(guò)以上處理,web輸出頁(yè)面中的源代碼就是型如: <input type="hidden" name="__SmartViewState" value="H4sIAPtPoNwA/81ce1PbWJb/ …… <input type="hidden" name="__VIEWSTATE" value="" /> 原來(lái)的隱藏域"__VIEWSTATE"值為空,而取而代之的是我們自己注冊(cè)的新的隱藏域"__SmartViewState"來(lái)存儲(chǔ)了經(jīng)過(guò)壓縮后的字符串,這樣以來(lái),提速效果是明顯的,結(jié)合我們的項(xiàng)目,象DG3G.COM的首頁(yè)原ViewState串值大約是28K,壓縮后為7K,而Acafa.com的首頁(yè)原ViewState串值大約是43K,壓縮后為8K。這樣的處理還是比較令人滿意的。當(dāng)然,如果你覺(jué)得還不夠徹底,你還可以把ViewState串存儲(chǔ)在Session中,不過(guò)這樣做,對(duì)服務(wù)器內(nèi)存的壓力將陡增(尤其是訪問(wèn)量較大的時(shí)候),建議還是不要輕易使用,如果你Web服務(wù)器內(nèi)存有個(gè)10G、8G的,不妨試試。下面給出相關(guān)修改代碼: 將上述LoadPageStateFromPersistenceMedium()方法體中的代碼: string viewState = Request.Form["__SmartViewState"]; 修改為: string viewState = Session["__SmartViewState"].ToString(); 同時(shí),將上述SavePageStateToPersistenceMedium()體中的代碼: this.RegisterHiddenField("__SmartViewState", Convert.ToBase64String(bytes)); 修改為: Session["__SmartViewState"] = Convert.ToBase64String(bytes); 末了,以上代碼和方案均來(lái)自VS2003開(kāi)發(fā)實(shí)踐,對(duì)VS2005是否合適,本人不做任何承諾,不過(guò)如果你能從以上方案中有所收獲,我將感到無(wú)比的高興。
摘自紅色黑客聯(lián)盟(www.) 原文:http://www./kf/201103/84651.html
|
|
來(lái)自: 遙遠(yuǎn)的橋zz > 《.net學(xué)習(xí)》