Two Methods for compressing ViewState of Asp. Net performance

Source: Internet
Author: User

Introduction to ASP. NET ViewState

ASP. NET ViewState is a new State service that allows developers to track the UI status based on each user. The auxiliary data is stored in a hidden field named _ VIEWSTATE.

Of course, ViewState has an important role in ASP. NET. If used properly, it can simplify page development and improve interaction between users and sites. If you ignore it, it can significantly increase the site response size. When the connection speed is slow, your response time will be slower. The browser will cause the ViewState to gradually increase the size of your page, resulting in performance problems. Therefore, the publication of ASP. NET 2.0 brings about some improvements to the ViewState mechanism, which makes ViewState easier to use and does not affect site performance. These improvements include reducing the number of codes, separating the travel status from the content into the status, and intelligently integrating data binding controls. You can solve this problem by disabling the control (EnableViewState = false) without maintaining the control status. However, it is necessary to maintain the control state in many cases, and the compressed ViewState helps improve the performance.

Download Sample project: ASPNET_ViewState.rar

Method 1: Use System. IO. Compression

System. IO. CompressionThe namespace contains classes that provide basic stream compression and decompression services.

This namespace contains two classes:

  • DeflateStream provides methods and attributes used to compress and decompress streams using the Deflate algorithm.
  • GZipStream provides methods and attributes for compressing and extracting streams.

In the Demo code below, we create a ViewStateCompression class, which contains two methods and returns byte [] data:

1. GZipStream compression/Decompression

Namespace ASPNET_ViewState.Code {public class ViewStateCompression {public ViewStateCompression () {// TODO: Add constructor logic here //} // comprete public static byte [] Compress (byte [] data) {MemoryStream output = new MemoryStream (); GZipStream gzip = new GZipStream (output, CompressionMode. compress, true); gzip. write (data, 0, data. length); gzip. close (); return output. toArray () ;}// Decompress public static byte [] Decompress (byte [] data) {MemoryStream input = new MemoryStream (); input. write (data, 0, data. length); input. position = 0; GZipStream gzip = new GZipStream (input, CompressionMode. decompress, true); MemoryStream output = new MemoryStream (); byte [] buff = new byte [64]; int read =-1; read = gzip. read (buff, 0, buff. length); while (read> 0) {output. write (buff, 0, read); read = gzip. read (buff, 0, buff. length);} gzip. close (); return output. toArray ();}}}

1B. DeflateStream compression/Decompression: Download the sample code in this article.

2. Execute the ViewStateCompression class

To use the ViewStateCompression compression and decompress Page ViewState function, we must rewrite the SavePageStateToPersistenceMedium () and LoadPageStateFromPersistenceMedium () methods of System. Web. UI. Page.

The SavePageStateToPersistenceMedium method can deserialize ViewState, which accepts parameters of a ViewState object.

The LoadPageStateFromPersistenceMedium method can serialize ViewState, which accepts a base64-encoded string parameter.

The following code is rewritten to create a BasePage class that inherits from System. Web. UI. Page:

Namespace ASPNET_ViewState.Code {public class BasePage: System. web. UI. page {public BasePage () {} protected override void initialize (object pageViewState) {LosFormatter losformatter = new LosFormatter (); StringWriter sw = new StringWriter (); losformatter. serialize (sw, pageViewState); string viewStateString = sw. toString (); byte [] B = Convert. fromBase64String (viewStateString); B = ViewStateCompression. compress (B); // ---- ClientScript. registerHiddenField ("_ ZIPSTATE", Convert. toBase64String (B); // compatible with ASP.. net ajax ViewState compression ScriptManager. registerHiddenField (this, "_ ZIPSTATE", Convert. toBase64String (B);} // serialize ViewState protected override object LoadPageStateFromPersistenceMedium () {string custState = Request. form ["_ ZIPSTATE"]; byte [] B = Convert. fromBase64String (custState); B = ViewStateCompression. decompress (B); LosFormatter losformatter = new LosFormatter (); return losformatter. deserialize (Convert. toBase64String (B ));}}}

After the preceding method, your ViewState may be reduced by 30-40%.

  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next Page

Related Article

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.