If you have the habit of viewing the HTML source code of the current page in IE, you may often see code snippets similar to the following:
- <input type="hidden" name="__VIEWSTATE" value=
"dDwtMzU5NzUyMTQ1O3Q8O2w8aTwwPjs+O2w8dDw7bDxpPDA+Oz47bDx0PDtsPG
- ……
Smart, you will certainly ask, what is this? What is the function? What is the relationship between it and this article? Let me hear from you.
In fact, this is the characteristic manifestation of MS's application of ViewState Technology in ASP. NET. To ensure that the page can still read the original status data of the server control after PostBack, ASP. NET app ViewState technology is essentially to use a Hidden type 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 introduces ASP. NET ViewState technology.
- Analysis of Theme functions in ASP. NET development skills
- ASP. NET Dynamic Compilation
- Analysis on ASP. NET supported by Apache
- Introduction to ASP. NET Server standard controls
- Analysis on SQL Server Database Backup Recovery in ASP. NET