有关于ViewState的二种压缩方式
此文仅收藏在此!
ViewState二种压缩方式
有关于GZipStream的应用
ViewStateHelper类
组件下载ICSharpCode
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
终:
一般都是牺牲带宽来减少服务器CPU的压力,因为随着网络的不断发展,带宽也会不断的发展,所以说宁愿让用户等页面的加载也不去加大服务器CPU的压力。为何这么说,是因为如果加大了CPU的压力,那就不是单用户等待页面加载的问题了,那会出现连锁反应。
ViewState二种压缩方式
未改进:不管什么时候都进行压缩(GZipStream方式压缩)
改进:可以设置当ViewState为多大时才启用压缩,和压缩比率的设置. public partial class ImproveCompressViewState : System.Web.UI.Page
{
/// <summary>
/// 设定序列化后的字符串长度为多少后启用压缩
/// </summary>
private static Int32 limitLength = 1096;
/// <summary>
/// 重写保存页的所有视图状态信息
/// </summary>
/// <param name="state">要在其中存储视图状态信息的对象</param>
protected override void SavePageStateToPersistenceMedium(Object state)
{
//实现一个用于将信息写入字符串的TextWriter
StringWriter writer = new StringWriter();
//序列化Web 窗体页的视图状态
LosFormatter format = new LosFormatter();
//将有限对象序列化(LOS) 格式化的对象转换为视图状态值
format.Serialize(writer, state);
//将序列化对象转成Base64字符串
String str = writer.ToString();
//设置是否启用了加密方式,默认情况下为不启用
Boolean useZip = false;
//判断序列化对象的字符串长度是否超出定义的长度界限
if (str.Length > limitLength)
{
//对于长度超出阶线的进行加密,同时将状态设为加密方式
useZip = true;
//将字节数组转换为Base64字符串
Byte[] bytes = ViewStateHelper.Compress(str);
str = Convert.ToBase64String(bytes);
}
//注册在页面储存ViewState状态的隐藏文本框,并将内容写入这个文本框
ClientScript.RegisterHiddenField("__myViewState", str);
//注册在页面储存是否启用压缩状态的文本框,并将启用状态写入这个文本框
ClientScript.RegisterHiddenField("__myViewState_ZIP", useZip.ToString().ToLower());
}
/// <summary>
/// 重写将所有保存的视图状态信息加载到页面对象
/// </summary>
/// <returns>保存的视图状态</returns>
protected override object LoadPageStateFromPersistenceMedium()
{
//使用Request方法获取序列化的ViewState字符串
String viewState = this.Request.Form.Get("__myViewState");
//使和Request方法获取当前的ViewState是否启用了压缩
String viewStateZip = this.Request.Form.Get("__myViewState_ZIP");
Byte[] bytes;
if (viewStateZip == "true")
{
bytes = ViewStateHelper.DeCompress(viewState);
}
else
{
//将ViewState的Base64字符串转换成字节
bytes = Convert.FromBase64String(viewState);
}
//序列化Web 窗体页的视图状态
LosFormatter format = new LosFormatter();
//将指定的视图状态值转换为有限对象序列化(LOS) 格式化的对象
return format.Deserialize(Convert.ToBase64String(bytes));
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
{
/// <summary>
/// 设定序列化后的字符串长度为多少后启用压缩
/// </summary>
private static Int32 limitLength = 1096;
/// <summary>
/// 重写保存页的所有视图状态信息
/// </summary>
/// <param name="state">要在其中存储视图状态信息的对象</param>
protected override void SavePageStateToPersistenceMedium(Object state)
{
//实现一个用于将信息写入字符串的TextWriter
StringWriter writer = new StringWriter();
//序列化Web 窗体页的视图状态
LosFormatter format = new LosFormatter();
//将有限对象序列化(LOS) 格式化的对象转换为视图状态值
format.Serialize(writer, state);
//将序列化对象转成Base64字符串
String str = writer.ToString();
//设置是否启用了加密方式,默认情况下为不启用
Boolean useZip = false;
//判断序列化对象的字符串长度是否超出定义的长度界限
if (str.Length > limitLength)
{
//对于长度超出阶线的进行加密,同时将状态设为加密方式
useZip = true;
//将字节数组转换为Base64字符串
Byte[] bytes = ViewStateHelper.Compress(str);
str = Convert.ToBase64String(bytes);
}
//注册在页面储存ViewState状态的隐藏文本框,并将内容写入这个文本框
ClientScript.RegisterHiddenField("__myViewState", str);
//注册在页面储存是否启用压缩状态的文本框,并将启用状态写入这个文本框
ClientScript.RegisterHiddenField("__myViewState_ZIP", useZip.ToString().ToLower());
}
/// <summary>
/// 重写将所有保存的视图状态信息加载到页面对象
/// </summary>
/// <returns>保存的视图状态</returns>
protected override object LoadPageStateFromPersistenceMedium()
{
//使用Request方法获取序列化的ViewState字符串
String viewState = this.Request.Form.Get("__myViewState");
//使和Request方法获取当前的ViewState是否启用了压缩
String viewStateZip = this.Request.Form.Get("__myViewState_ZIP");
Byte[] bytes;
if (viewStateZip == "true")
{
bytes = ViewStateHelper.DeCompress(viewState);
}
else
{
//将ViewState的Base64字符串转换成字节
bytes = Convert.FromBase64String(viewState);
}
//序列化Web 窗体页的视图状态
LosFormatter format = new LosFormatter();
//将指定的视图状态值转换为有限对象序列化(LOS) 格式化的对象
return format.Deserialize(Convert.ToBase64String(bytes));
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
有关于GZipStream的应用
protected override void SavePageStateToPersistenceMedium(object state)
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, state);
String viewState = writer.ToString();
Byte[] data = Convert.FromBase64String(viewState);
Byte[] compressedData = ViewStateHelper.Compress(data);
String str = Convert.ToBase64String(compressedData);
ClientScript.RegisterHiddenField("__MYVIEWSTATE", str);
}
protected override object LoadPageStateFromPersistenceMedium()
{
String viewState = Request.Form["__MYVIEWSTATE"];
Byte[] data = Convert.FromBase64String(viewState);
Byte[] unCompressedData = ViewStateHelper.DeCompress(data);
String str = Convert.ToBase64String(unCompressedData);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(str);
}
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, state);
String viewState = writer.ToString();
Byte[] data = Convert.FromBase64String(viewState);
Byte[] compressedData = ViewStateHelper.Compress(data);
String str = Convert.ToBase64String(compressedData);
ClientScript.RegisterHiddenField("__MYVIEWSTATE", str);
}
protected override object LoadPageStateFromPersistenceMedium()
{
String viewState = Request.Form["__MYVIEWSTATE"];
Byte[] data = Convert.FromBase64String(viewState);
Byte[] unCompressedData = ViewStateHelper.DeCompress(data);
String str = Convert.ToBase64String(unCompressedData);
LosFormatter formatter = new LosFormatter();
return formatter.Deserialize(str);
}
ViewStateHelper类
public static class ViewStateHelper
{
未使用压缩组件的Compress/DeCompress
使用压缩组件的Compress/DeCompress
}
{
未使用压缩组件的Compress/DeCompress
使用压缩组件的Compress/DeCompress
}
组件下载ICSharpCode
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
终:
一般都是牺牲带宽来减少服务器CPU的压力,因为随着网络的不断发展,带宽也会不断的发展,所以说宁愿让用户等页面的加载也不去加大服务器CPU的压力。为何这么说,是因为如果加大了CPU的压力,那就不是单用户等待页面加载的问题了,那会出现连锁反应。