惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 最新话题
N
News | PayPal Newsroom
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Help Net Security
Help Net Security
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园_首页
博客园 - 司徒正美
The Cloudflare Blog
D
DataBreaches.Net
Jina AI
Jina AI
L
LINUX DO - 热门话题
宝玉的分享
宝玉的分享
Project Zero
Project Zero
量子位
Spread Privacy
Spread Privacy
Cisco Talos Blog
Cisco Talos Blog
J
Java Code Geeks
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
Hugging Face - Blog
Hugging Face - Blog
T
Threat Research - Cisco Blogs
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
AI
AI
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
Attack and Defense Labs
Attack and Defense Labs
V
V2EX
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
Cyberwarzone
Cyberwarzone

博客园 - Mack.Z

压缩ASP.NET中的ViewState的改进方法 - Mack.Z - 博客园 MSPlus.TabControl V1.1 版本发布 发布MSPlus TabControl WebControl V1.0.0710 版本 Html Convert Image (Html2Image) Gaia(MasterPage)我的第一个开源控件 MSN Winks 文件的解密 学习SPRING中的一个疑惑.请教一下各位 发布MSPlus DatePicker WebControl V2.0.1201 版本 发布MSPlus ToolBar&Menu WebControl V1.1.0910 版本 MSPlus DatePicker WebControl FreeVersion 1.1.0906 发布啦! 关于MSPlus控件下载后用VS.NET打开提示目录不对的解决方法 - Mack.Z - 博客园 MSPlus ToolBar&Menu WebControl FreeVersion 1.1.0830 发布拉 - Mack.Z [原创]复合控件中如何将客户端的处理结果通知服务器端的解决办法 [原创]屏蔽.NET自定义开发组件中的属性 MSPlus DataList Control(大数据量时的分页演示) MSPlus DataList Control 1.1.0818(个人版) 发布! MSPLUS-DropDownList Control [DEMO Download] 想开发.Net Server Controls 的初学者必读资料 MSPlus-ToolBar Control - Mack.Z - 博客园
压缩ASP.NET中的ViewState
Mack.Z · 2005-07-27 · via 博客园 - Mack.Z

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

以下是结合CSharpZipLib对ViewState进行压缩的方法.

MSPlus.Web.UI.Page 源码:

using System;
using System.Web.UI;
using System.IO;
using ICSharpCode.SharpZipLib.Zip.Compression;

namespace MSPlus.Web.UI
{
    
/// <summary>
    
/// 压缩ViewState By Mack.Z (MSPlus)
    
/// </summary>

    public class Page : System.Web.UI.Page
    
{

        
protected override void SavePageStateToPersistenceMedium(Object pViewState)
        
{
            LosFormatter mFormat 
= new LosFormatter();
            StringWriter mWriter 
= new StringWriter();

            mFormat.Serialize(mWriter, pViewState); 
            String mViewStateStr 
= mWriter.ToString(); 

            
byte[] pBytes = System.Convert.FromBase64String(mViewStateStr);

            pBytes 
= Compress(pBytes); 

            String vStateStr 
= System.Convert.ToBase64String(pBytes); 

            RegisterHiddenField(
"__MSPVSTATE", vStateStr);
        }


        
protected override Object LoadPageStateFromPersistenceMedium()
        
{
            String vState 
= this.Request.Form.Get("__MSPVSTATE");

            
byte[] pBytes = System.Convert.FromBase64String(vState);

            pBytes 
= DeCompress(pBytes);

            LosFormatter mFormat 
= new LosFormatter();

            
return mFormat.Deserialize(System.Convert.ToBase64String(pBytes));
        }



        
public static byte[] Compress(byte[] pBytes) 
        

            MemoryStream mMemory 
= new MemoryStream();

            Deflater mDeflater 
= new Deflater(ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION);
            ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream mStream 
= new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(mMemory,mDeflater,131072);

            mStream.Write(pBytes,
0,pBytes.Length);
            mStream.Close();

            
return mMemory.ToArray();
        }
 


        
public static byte[] DeCompress(byte[] pBytes) 
        

            ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream mStream 
= new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(pBytes));
            
            MemoryStream mMemory 
= new MemoryStream();
            Int32 mSize;

            
byte[] mWriteData = new byte[4096];

            
while(true)
            
{
                mSize 
= mStream.Read(mWriteData, 0, mWriteData.Length);
                
if (mSize > 0)
                
{
                    mMemory.Write(mWriteData, 
0, mSize);
                }

                
else
                
{
                     
break;
                }

            }


            mStream.Close();
            
return mMemory.ToArray();
        }
 

    }

}

使用方法(ASPX.CS页面继承MSPlus.Web.UI.Page):public class PageClass : System.Web.UI.Page

测试下来的结果:压缩后的ViewState只有原来的20%!


我作了一个小小的测试.先后用同一个页面加密和不加密的情况下,刷新页面观测w3wp.exe的CPU占用情况.此页面的ViewState大小为3.996 Bytes

w3wp.exe 以下是每次刷新的时CPU的情况

05 03 05 03 06 05 05 08 03 05 03 不加密,平均是 4.6
05 05 06 05 05 03 08 02 03 05 06 加密,平均是5.3