庖丁解牛Asp.net3.5控件和组件开发技术系列—页面状态机制(四)
og.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx /// </summary> public class CompressHelp { //序列化工具,LosFormatter是页面默认的序列器 private static LosFormatter _formatter = new LosFormatter(); /// <summary> /// 解压并反序列化状态内容 /// </summary> /// <param name="stateString">从客户端取回的页面状态字符串</param> /// <returns>还原后的页面状态Pair对象</returns> public static object Decompress(string stateString) { byte[] buffer = Convert.FromBase64String(stateString); MemoryStream ms = new MemoryStream(buffer); GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress); MemoryStream msReader = new MemoryStream(); buffer = new byte[0x1000]; while (true) { int read = zipStream.Read(buffer, 0, buffer.Length); if (read <= 0) { break; } msReader.Write(buffer, 0, read); } zipStream.Close(); ms.Close(); msReader.Position = 0; buffer = msReader.ToArray(); stateString = Convert.ToBase64String(buffer); return _formatter.Deserialize(stateString); } /// <summary> /// 序列化并压缩状态内容 /// </summary> /// <param name="state">页面状态</param> /// <returns>结果字符串</returns> public static string Compress(object state) { StringWriter writer = new StringWriter(); _formatter.Serialize(writer, state); string stateString = writer.ToString(); writer.Close(); byte[] buffer = Convert.FromBase64String(stateString); MemoryStream ms = new MemoryStream(); GZipStream zipStream=new GZipStream(ms,CompressionMode.Compress,true); zipStream.Write(buffer, 0, buffer.Length); zipStream.Close(); buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); ms.Close(); stateString = Convert.ToBase64String(buffer); return stateString; } } 最后,把定义的KingControls.CompressPageStatePersister应用到页面上(页面名称为CompressPageState.aspx,内容仍然使用前面的控件状态控件的测试页面内容)。代码如下:
OK,在浏览器中运行页面,可以看到,页面中的__VIEWSTATE值由原来的:
|
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |