压缩ASP.NET中的ViewState的超强改进方法

本文介绍了一种针对ASP.NET Web窗体中ViewState数据的压缩方法,通过判断ViewState字符串长度来决定是否启用压缩,以此减少网络传输的数据量,提高网页加载速度。
None.gifusing System;
None.gif
using System.IO;
None.gif
using System.Web;
None.gif
using System.Web.UI;
None.gif
using ICSharpCode.SharpZipLib;
None.gif
None.gif
namespace WebGenTestApp.CommClass
ExpandedBlockStart.gifContractedBlock.gifdot.gif
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
InBlock.gif    
/// PageClass 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ViewStateCompressClass : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 设定序列化后的字符串长度为多少后启用压缩
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static Int32 LimitLength = 1096;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 设定压缩比率,压缩比率越高性消耗也将增大
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static Int32 ZipLevel = ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 重写保存页的所有视图状态信息
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="pViewState">要在其中存储视图状态信息的对象</param>

InBlock.gif        protected override void SavePageStateToPersistenceMedium(Object pViewState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
//实现一个用于将信息写入字符串的 TextWriter
InBlock.gif
            StringWriter mWriter = new StringWriter();
InBlock.gif
InBlock.gif            
//序列化 Web 窗体页的视图状态
InBlock.gif
            LosFormatter mFormat = new LosFormatter();
InBlock.gif
InBlock.gif            
//将有限对象序列化 (LOS) 格式化的对象转换为视图状态值
InBlock.gif
            mFormat.Serialize(mWriter, pViewState);
InBlock.gif
InBlock.gif            
//将序列化对象转成Base64字符串
InBlock.gif
            String vStateStr = mWriter.ToString();
InBlock.gif
InBlock.gif            
//设置是否启用了加密方式,默认情况下为不启用
InBlock.gif
            Boolean mUseZip = false;
InBlock.gif
InBlock.gif            
//判断序列化对象的字符串长度是否超出定义的长度界限
InBlock.gif
            if (vStateStr.Length > LimitLength)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
//对于长度超出阶线的进行加密,同时将状态设为加密方式
InBlock.gif
                mUseZip = true;
InBlock.gif
InBlock.gif                Byte[] pBytes 
= Compress(vStateStr);
InBlock.gif
InBlock.gif                
//将字节数组转换为Base64字符串
InBlock.gif
                vStateStr = System.Convert.ToBase64String(pBytes);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//注册在页面储存ViewState状态的隐藏文本框,并将内容写入这个文本框
InBlock.gif
            RegisterHiddenField("__MSPVSTATE", vStateStr);
InBlock.gif
InBlock.gif            
//注册在页面储存是否启用压缩状态的文本框,并将启用状态写入这个文本框
InBlock.gif
            RegisterHiddenField("__MSPVSTATE_ZIP", mUseZip.ToString().ToLower());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 对字符串进行压缩
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="pViewState">ViewState字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回流的字节数组</returns>

InBlock.gif        public static Byte[] Compress(String pViewState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
//将存储状态的Base64字串转换为字节数组
InBlock.gif
            Byte[] pBytes = System.Convert.FromBase64String(pViewState);
InBlock.gif
InBlock.gif            
//创建支持内存存储的流
InBlock.gif
            MemoryStream mMemory = new MemoryStream();
InBlock.gif
InBlock.gif            Deflater mDeflater 
= new Deflater(ZipLevel);
InBlock.gif            ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream mStream 
= new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(mMemory, mDeflater, 131072);
InBlock.gif
InBlock.gif            mStream.Write(pBytes, 
0, pBytes.Length);
InBlock.gif            mStream.Close();
InBlock.gif
InBlock.gif            
return mMemory.ToArray();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 重写将所有保存的视图状态信息加载到页面对象
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>保存的视图状态</returns>

InBlock.gif        protected override Object LoadPageStateFromPersistenceMedium()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
//使用Request方法获取序列化的ViewState字符串
InBlock.gif
            String mViewState = this.Request.Form.Get("__MSPVSTATE");
InBlock.gif            
//使和Request方法获取当前的ViewState是否启用了压缩
InBlock.gif
            String mViewStateZip = this.Request.Form.Get("__MSPVSTATE_ZIP");
InBlock.gif
InBlock.gif            Byte[] pBytes;
InBlock.gif
InBlock.gif            
if (mViewStateZip == "true")
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                pBytes 
= DeCompress(mViewState);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                
//将ViewState的Base64字符串转换成字节
InBlock.gif
                pBytes = System.Convert.FromBase64String(mViewState);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//序列化 Web 窗体页的视图状态
InBlock.gif
            LosFormatter mFormat = new LosFormatter();
InBlock.gif
InBlock.gif            
//将指定的视图状态值转换为有限对象序列化 (LOS) 格式化的对象
InBlock.gif
            return mFormat.Deserialize(System.Convert.ToBase64String(pBytes));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// 解压缩ViewState字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="pViewState">ViewState字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回流的字节数组</returns>

InBlock.gif        public static Byte[] DeCompress(String pViewState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
dot.gif{
InBlock.gif            
//将Base64字符串转换为字节数组
InBlock.gif
            Byte[] pBytes = System.Convert.FromBase64String(pViewState);
InBlock.gif
InBlock.gif            ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream mStream 
= new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(pBytes));
InBlock.gif
InBlock.gif            
//创建支持内存存储的流
InBlock.gif
            MemoryStream mMemory = new MemoryStream();
InBlock.gif            Int32 mSize;
InBlock.gif
InBlock.gif            Byte[] mWriteData 
= new Byte[4096];
InBlock.gif
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
dot.gif{
InBlock.gif                mSize 
= mStream.Read(mWriteData, 0, mWriteData.Length);
InBlock.gif                
if (mSize > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    mMemory.Write(mWriteData, 
0, mSize);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            mStream.Close();
InBlock.gif            
return mMemory.ToArray();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值