ViewState压缩
/// <summary> ///CompressViewState 的摘要说明 /// </summary> public class CompressViewState:System.Web.UI.Page { public CompressViewState() { // //TODO: 在此处添加构造函数逻辑 // } protected override object LoadPageStateFromPersistenceMedium() { PageStatePersister pageStatePersister = this.PageStatePersister; pageStatePersister.Load(); string vState = pageStatePersister.ViewState.ToString(); byte[] pBytes = System.Convert.FromBase64String(vState); pBytes = Decompress(pBytes); LosFormatter mFormat = new LosFormatter(); object ViewState = mFormat.Deserialize(System.Convert.ToBase64String(pBytes)); return new Pair(pageStatePersister.ControlState, ViewState); } protected override void SavePageStateToPersistenceMedium(object pViewState) { Pair pair; PageStatePersister pageStatePersister = this.PageStatePersister; object ViewState; if (pViewState is Pair) { pair = ((Pair)pViewState); pageStatePersister.ControlState = pair.First; ViewState = pair.Second; } else { ViewState = pViewState; } LosFormatter mFormat = new LosFormatter(); StringWriter mWriter = new StringWriter(); mFormat.Serialize(mWriter, ViewState); String mViewStateStr = mWriter.ToString(); byte[] pBytes = System.Convert.FromBase64String(mViewStateStr); pBytes = Compress(pBytes); String vStateStr = System.Convert.ToBase64String(pBytes); pageStatePersister.ViewState = vStateStr; pageStatePersister.Save(); } /// <summary> /// This Methhod takes the byte stream as parameter /// and return a compressed bytestream. /// For compression it uses GZipStream /// </summary> /// <param name="b"></param> /// <returns></returns> private byte[] Compress(byte[] b) { MemoryStream ms = new MemoryStream(); GZipStream zs = new GZipStream(ms, CompressionMode.Compress, true); zs.Write(b, 0, b.Length); zs.Close(); return ms.ToArray(); } /// <summary> /// This methhod takes the compressed byte stream as parameter /// and return a decompressed bytestream. /// </summary> /// <param name="b"></param> /// <returns></returns> private byte[] Decompress(byte[] b) { MemoryStream ms = new MemoryStream(); GZipStream zs = new GZipStream(new MemoryStream(b), CompressionMode.Decompress, true); byte[] buffer = new byte[4096]; int size; while (true) { size = zs.Read(buffer, 0, buffer.Length); if (size > 0) ms.Write(buffer, 0, size); else break; } zs.Close(); return ms.ToArray(); } }
http://www.codeproject.com/Articles/101888/ViewState-Various-ways-to-reduce-performance-overh
天行健,君子以自强不息