The characteristics of MS's application of ViewState Technology in ASP. NET. ASP. NET application ViewState technology, in essence, ViewState technology uses a Hidden form field named "_ VIEWSTATE by default to store and transmit data. The data is a Base64 encoded string value after serialization, and in the method Page. savePageStateToPersistenceMedium is saved before output and is stored by Page. loadPageStateFromPersistenceMedium load ). Although we can easily disable round-trip transmission of the data at three levels:
In Machine. config, set <pages enableViewStateMac = 'false'/>
Set <pages enableViewStateMac = 'false'/>
Set <enableViewStateMac = 'false' %> on a single Page or set Page. EnableViewStateMac = false by code;
However, if we can completely disable ViewState to solve the data transmission burden without side effects, so MS architects will not be so stupid as to be so cute, what can they do with it ?), Because we often cannot solve this transmission burden problem by simply disabling it, we can only set another path to make it as small as possible in the round-trip network, so compression is our first choice. You only need to reload the SavePageStateToPersistenceMedium method and LoadPageStateFromPersistenceMedium method of the Page class, and compress and decompress the data in the reload method. The class GZipInputStream and GZipOutputStream provided by the open-source project SharpZipLib enter our field of view. For convenience, you may write a class CompressionHelper. The Code is as follows:
- Using System. IO;
- Using ICSharpCode. SharpZipLib. GZip;
-
- Namespace Ycweb. Components
- {
- /**////<Summary>
- /// Summary description for CompressionHelper.
- ///</Summary>
- Public class CompressionHelper
- {
- Public CompressionHelper ()
- {
- //
- // TODO: Add constructor logic here
- //
- }
-
- /**////<Summary>
- /// Compress data
- ///</Summary>
- ///<Param Name="Data">Byte array to be compressed</Param>
- ///<Returns>Compressed byte array</Returns>
- Public static byte [] CompressByte (byte [] data)
- {
- MemoryStreamMS=NewMemoryStream ();
- StreamS=NewGZipOutputStream (MS );
- S. Write (data, 0, data. Length );
- S. Close ();
- Return ms. ToArray ();
- }
-
- /**////<Summary>
- /// Decompress the data
- ///</Summary>
- ///<Param Name="Data">Byte array to be decompressed</Param>
- ///<Returns>Decompressed byte array</Returns>
- Public static byte [] DeCompressByte (byte [] data)
- {
- Byte []WriteData=NewByte [2, 2048];
- MemoryStreamMS=NewMemoryStream (data );
- StreamSm=NewGZipInputStream (MS) as Stream;
- MemoryStreamOutStream=NewMemoryStream ();
- While (true)
- {
- IntSize=Sm. Read (writeData, 0, writeData. Length );
- If (size>0)
- {
- OutStream. Write (writeData, 0, size );
- }
- Else
- {
- Break;
- }
- }
- Sm. Close ();
- Byte []OutArr=OutStream. ToArray ();
- OutStream. Close ();
- Return outArr;
- }
- }
- }
The above describes ASP. NET ViewState technology.
- DataList and Repeater controls of ASP. NET
- Analysis of IIS ing in ASP. NET
- Overview ASP. NET status types
- Introduction to ASP. NET and Web servers
- EnableViewState attribute of ASP. NET