If you have the habit of viewing the HTML source code of the current page you are browsing in IE, you may often see code snippets similar to the following:
<input type="hidden" name="__VIEWSTATE" value="dDwtMzU5NzUyMTQ1O3Q8O2w8aTwwPjs+O2w8dDw7bDxpPDA+Oz47bDx0PDtsPG
...
If you are smart, you will definitely ask, what is this? What does it do? How does it relate to this article? Dear readers, please listen to me slowly.
In fact, this is the characteristic performance of MS's application of ViewState technology in Asp.net. In order for the page to still be able to read the original state data of the server control after PostBack, Asp.net uses ViewState technology, which essentially uses a Hidden type form field with the default name of "__VIEWSTATE" to save and transfer data. (These data are serialized Base64 encoded string values and are saved before output by the method Page.SavePageStateToPersistenceMedium and loaded by Page.LoadPageStateFromPersistenceMedium) Although we can easily disable the round trip of these data through three levels. Pass:
Machine level set <pages enableViewStateMac='false' /> in machine.config
At the Application level, set <pages enableViewStateMac='false' /> in Web Applicin's web.config
At the single page level, set < %@Page enableViewStateMac='false' %> in the page or set Page.EnableViewStateMac = false through code;
However, if we can completely solve the data transmission burden by disabling ViewState without causing side effects, then MS architects will not be so stupid as to be so cute (what is the use of leaving it as a dispensable thing?), Because we often cannot solve this transmission burden problem by simply disabling it, we can only find another path to make the transmission volume in the network round trip as small as possible, so compression has become our first choice. As long as we overload the SavePageStateToPersistenceMedium() method and LoadPageStateFromPersistenceMedium() method of the Page class, and compress and decompress the data in the overloaded method. The classes GZipInputStream and GZipOutputStream provided by the open source project SharpZipLib have come into our view. For convenience, we might as well write a class CompressionHelper with the following code:
1using System.IO;
2using ICSharpCode.SharpZipLib.GZip;
3
4namespace Ycweb.Components
5{
6 /**//// <summary>
7 /// Summary description for CompressionHelper.
8 /// </summary>
9 public class CompressionHelper
10 {
11 publicCompressionHelper()
12 {
13 //
14 // TODO: Add constructor logic here
15 //
16}
17
18 /**//// <summary>
19 /// Compress data
20 /// </summary>
21 /// <param name="data">Byte array to be compressed</param>
22 /// <returns>Compressed byte array</returns>
23 public static byte[] CompressByte(byte[] data)
twenty four {
25 MemoryStream ms = new MemoryStream();
26 Stream s=new GZipOutputStream(ms);
27 s.Write(data, 0, data.Length);
28 s.Close();
29 return ms.ToArray();
30}
31
32 /**//// <summary>
33 /// Decompress data
34 /// </summary>
35 /// <param name="data">Byte array to be decompressed</param>
36 /// <returns>Decompressed byte array</returns>
37 public static byte[] DeCompressByte(byte[] data)
38 {
39 byte[] writeData = new byte[2048];
40 MemoryStream ms= new MemoryStream( data );
41 Stream sm = new GZipInputStream(ms) as Stream;
42 MemoryStream outStream = new MemoryStream();
43 while (true)
44 {
45 int size = sm.Read(writeData,0, writeData.Length );
46 if (size >0)
47 {
48 outStream.Write(writeData,0,size);
49 }
50 else
51 {
52 break;
53}
54 }
55 sm.Close();
56 byte[] outArr = outStream.ToArray();
57 outStream.Close();
58 return outArr;
59 }
60 }
61} Then we overload the methods LoadPageStateFromPersistenceMedium() and SavePageStateToPersistenceMedium(Object viewState) in the page control base class derived from class Page. The code is as follows:
1Load & Save ViewState Data#region Load & Save ViewState Data
2 protected override object LoadPageStateFromPersistenceMedium()
3 {
4//Read data from the hidden domain __SmartViewState registered by yourself
5 string viewState = Request.Form["__SmartViewState"];
6 byte[] bytes = Convert.FromBase64String(viewState);
7 //Call the static method CompressionHelper.DeCompressByte() provided above to decompress the data
8 bytes = CompressionHelper.DeCompressByte(bytes);
9 LosFormatter formatter = new LosFormatter();
10 return formatter.Deserialize(Convert.ToBase64String(bytes));
11
12}
13
14 protected override void SavePageStateToPersistenceMedium(Object viewState)
15 {
16 LosFormatter formatter = new LosFormatter();
17 StringWriter writer = new StringWriter();
18 formatter.Serialize(writer, viewState);
19 string viewStateString = writer.ToString();
20 byte[] bytes = Convert.FromBase64String(viewStateString);
21 //Call the static method CompressionHelper.CompressByte() provided above to compress the data
22 bytes = CompressionHelper.CompressByte(bytes);
twenty three
24 //Register a new hidden field __SmartViewState, you can also define it yourself
25 this.RegisterHiddenField("__SmartViewState", Convert.ToBase64String(bytes));
26}
27#endregion
After the above processing, the source code in the web output page is like:
<input type="hidden" name="__SmartViewState" value="H4sIAPtPoNwA/81ce1PbWJb/ ……
<input type="hidden" name="__VIEWSTATE" value="" />
The original hidden field "__VIEWSTATE" has an empty value, and is replaced by a new hidden field "__SmartViewState" registered by ourselves to store the compressed string. In this way, the speed-up effect is obvious. Combined with our project, For example, the original ViewState string value of the homepage of DG3G.COM is about 28K, and it is 7K after compression, while the original ViewState string value of the homepage of Acafa.com is about 43K, and it is 8K after compression. This kind of processing is quite satisfactory. Of course, if you feel that it is not thorough enough, you can also store the ViewState string in the Session. However, if you do this, the pressure on the server memory will increase sharply (especially when the number of visits is large). It is recommended not to use it lightly. If Your web server has 10G or 8G memory, you might as well give it a try. The relevant modified code is given below:
Change the code in the above LoadPageStateFromPersistenceMedium() method body:
string viewState = Request.Form["__SmartViewState"];
Modify to:
string viewState = Session["__SmartViewState"].ToString();
At the same time, add the code in the above SavePageStateToPersistenceMedium() body:
this.RegisterHiddenField("__SmartViewState", Convert.ToBase64String(bytes));
Modify to:
Session["__SmartViewState"] = Convert.ToBase64String(bytes);
Finally, the above codes and solutions are all from VS2003 development practice. I do not make any promises about whether VS2005 is suitable. However, if you can gain something from the above solutions, I will Will be extremely happy.