ViewState for optimizing ASP. NET application performance

Source: Internet
Author: User

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 + o2w8ddw7bdxp PDA + 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 performance 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 uses the 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:

Set <pages enableViewStateMac = false/> in Machine. config at the machine level.
Set <pages enableViewStateMac = false/> in Web. config of web Applicatin at the Application level.
Set <% @ Page enableViewStateMac = false %> or set Page. EnableViewStateMac = false through code;
  
However, if we can completely disable ViewState to solve the data transmission burden without side effects, then MS architects will not be so cute (what can be left ?), 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:

1 using System. IO;
2 using ICSharpCode. SharpZipLib. GZip;
3
4 namespace Ycweb. Components
5 {
6/** // <summary>
7 // Summary description for CompressionHelper.
8 /// </summary>
9 public class CompressionHelper
10 {
11 public CompressionHelper ()
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)
24 {
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 the 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 [1, 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 load the methods LoadPageStateFromPersistenceMedium () and SavePageStateToPersistenceMedium (Object viewState) in the Page control base class derived from the Page class. The Code is as follows:
1 Load & Save ViewState Data # region Load & Save ViewState Data
2 protected override object LoadPageStateFromPersistenceMedium ()
3 {
4 // read data from your own hidden domain _ SmartViewState
5 string viewState = Request. Form ["_ SmartViewState"];
6 byte [] bytes = Convert. FromBase64String (viewState );
7 // call the static method CompressionHelper. DeCompressByte () provided above to decompress the package

Data
8 bytes = CompressionHelper. DeCompressByte (bytes );
9 LosFormatter formatter = new LosFormatter ();
10 return formatter. Deserialize (Convert. ToBase64String (bytes ));
11
12}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.