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

推荐订阅源

F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Secure Thoughts
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
NISL@THU
NISL@THU
博客园 - 司徒正美
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
B
Blog
The GitHub Blog
The GitHub Blog
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
Martin Fowler
Martin Fowler
博客园 - 叶小钗
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
S
Securelist
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
T
Threat Research - Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
Cloudbric
Cloudbric
The Cloudflare Blog
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Hacker News: Front Page
腾讯CDC
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
V2EX - 技术
V2EX - 技术
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
Google Online Security Blog
Google Online Security Blog
T
The Blog of Author Tim Ferriss

博客园 - Ansel

如何做到某个网站被访问? 也谈项目经理 项目团队几个发展阶段总结 如何当好一个项目经理(引用) 微创面试归来话感想 .NET框架垃圾回收机制 二分法——查找、排序以及库函数bsearch的用法 关于web.config - Ansel - 博客园 使用C#和MSMQ开发消息处理程序 访问数据库时如何解决并发问题 very nice to have talked with you via phone 利用程序动态管理Web.config文件 使用ASP.NET Global.asax 文件--基础篇 ASP.NET 2.0 的内部变化 .Net调用Java的WebService之亲身体验 服务器端异步 Web 方法 在 ASP.NET 中支持数据库缓存相关性 关于 .NET Passport 身份验证 用.Net开发Windows服务初探
压缩ASP.NET中的ViewState
Ansel · 2005-08-31 · via 博客园 - Ansel

服务器端控件带来的好外我在这就不多说了,但多过的使用服务器端件保存控件的状态会带来大量的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