10 ways to improve. NET page performance

Source: Internet
Author: User
Tags md5 encryption delete cache

1. Use HTTP module to control the life cycle of the page.

2. Custom Response.filter get output stream stream to generate static content of dynamic pages (disk cache).

3. Page gzip compression.

4.outputcache output page Caching programmatically.

5. Delete the page blank string. (similar to Google)

6. Completely remove ViewState.

7. Delete the garbage NamingContainer generated by the server control.

8. Use scheduled tasks to generate pages on time. (The implementation of this practice is not included in this article)

9.JS,CSS tutorial compression, merging, caching, picture caching. (This article does not include the implementation of this practice)

10. Cache damage. (does not include implementation of the 9th procedure)

For the above approach, we first need an HTTP module, which is the portal and core of the entire page process.

A, custom response.filter get output stream stream generate static content for dynamic pages (disk cache)

As we can see in the code below, we use Request.rawurl as the cache base because it can contain arbitrary querystring variables, and then we use MD5 encryption Rawurl get the server local filename variable, and then instantiate a fileinfo operation of the file, If the file's last build time is less than 7 days, we use the. net2.0 new TransmitFile method to send the static content of the stored file to the browser. If the file does not exist, we pass the Response.filter stream to the Commonfilter class and use FileStream to write the contents of the dynamic page to the static file.


Second, the page gzip compression

The page gzip compression is almost one of the few ways to explain high-performance Web programs, because using gzip compression can reduce the number of bytes sent by the server, allowing customers to feel the speed of web pages and reduce bandwidth usage. Of course, there is also a client browser that supports it. So what we're going to do is if the client supports GZIP, we send gzip-compressed content, and if not, we send the contents of the static file directly. Fortunately, the modern browser ie6.7.8.0, Firefox, and so on are supported by Gzip.

To implement this functionality, we need to overwrite the Application_BeginRequest event above:


Three, OutputCache programming way Output page cache

ASP tutorial. NET built-in OutputCache cache can cache content in three places: Web servers, proxy servers, and browsers. When a user accesses a page that is set to OutputCache, the ASP.net tutorial writes the result to the output cache after the MSIL, and then when the user accesses a page in the same path, when it is sent to the browser, asp.net will send the contents of the cache directly without the process of. aspx compilation and execution of MSIL, so although the program itself does not improve its efficiency, the page loading speed is improved.

In order to achieve this function, we continue to rewrite the above Application_BeginRequest event, we transmitfile the path of the page in OutputCache programming way to cache:


Implementing Commonfilter filtering ViewState, filtering namingcontainer, blank strings, and generating cached files for disk

We passed the Response.filter stream object to the Commonfilter class:

First, we use the first stream write method to create a cached file for the disk, the code is as follows, in which only the initialization constructor, the write method, the Close method is useful, where the FileStream field is the action object that generated the static file:


V. Cache destruction

After the implementation of the above program, the Web page has been cached in the client, if the user access to the site has been cached pages, the page will be 0 of the requested speed load page. However, if some data is updated in the background, the foreground user will not be able to see the latest data in time, so to change this situation, we must destroy the cache. According to our program as above, we only need to do 2 steps to destroy the cache: update the temporary files on the server, delete the OutputCache pages.

Update files on the server we only need to delete this file, when a user first visit the page will automatically generate, of course, you can also use the program to delete after the build:

Update files
To delete the cached item associated with the outputcache, the code is as follows, we only need to guarantee the parameter of the method, which means the absolute path of the page is correct and the path is not available. /Such a relative path:

Delete Cache

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.