测试压缩ASP.NET中的ViewState

本文介绍了一种利用CSharpZipLib库对ASP.NET中的ViewState进行压缩的方法,以减少ViewState大小,从而提高页面加载速度并改善服务器性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务器端控件带来的好外我在这就不多说了,但多过的使用服务器端件保存控件的状态会带来大量的ViewState的情况大家一定遇到过吧.过多的ViewState会很大程度上降低页面的加载速度制成服务器端的性能下降.

以下是结合CSharpZipLib对ViewState进行压缩的方法.
using System;
None.gif
using System.Web.UI;
None.gif
using System.IO;
None.gif
using ICSharpCode.SharpZipLib.Zip.Compression;
None.gif
None.gif
namespace MSPlus.Web.UI
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 压缩ViewState By Mack.Z (MSPlus)
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Page : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
protected override void SavePageStateToPersistenceMedium(Object pViewState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            LosFormatter mFormat 
= new LosFormatter();
InBlock.gif            StringWriter mWriter 
= new StringWriter();
InBlock.gif
InBlock.gif            mFormat.Serialize(mWriter, pViewState); 
InBlock.gif            String mViewStateStr 
= mWriter.ToString(); 
InBlock.gif
InBlock.gif            
byte[] pBytes = System.Convert.FromBase64String(mViewStateStr);
InBlock.gif
InBlock.gif            pBytes 
= Compress(pBytes); 
InBlock.gif
InBlock.gif            String vStateStr 
= System.Convert.ToBase64String(pBytes); 
InBlock.gif
InBlock.gif            RegisterHiddenField(
"__MSPVSTATE", vStateStr);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override Object LoadPageStateFromPersistenceMedium()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String vState 
= this.Request.Form.Get("__MSPVSTATE");
InBlock.gif
InBlock.gif            
byte[] pBytes = System.Convert.FromBase64String(vState);
InBlock.gif
InBlock.gif            pBytes 
= DeCompress(pBytes);
InBlock.gif
InBlock.gif            LosFormatter mFormat 
= new LosFormatter();
InBlock.gif
InBlock.gif            
return mFormat.Deserialize(System.Convert.ToBase64String(pBytes));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static byte[] Compress(byte[] pBytes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            MemoryStream mMemory 
= new MemoryStream();
InBlock.gif
InBlock.gif            Deflater mDeflater 
= new Deflater(ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION);
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        
public static byte[] DeCompress(byte[] pBytes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream mStream 
= new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(pBytes));
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{
InBlock.gif                mSize 
= mStream.Read(mWriteData, 0, mWriteData.Length);
InBlock.gif                
if (mSize > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    mMemory.Write(mWriteData, 
0, mSize);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.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}

转载于:https://www.cnblogs.com/babayaluo/archive/2006/07/06/443926.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值