Webpage optimization Series 1: Merge file requests (asp.net)

Source: Internet
Author: User

Recently, I have learned how to optimize my website. I 'd like to share my learning achievements with you through this series of blog posts. If you have any questions, please let me know more.

First, we recommend a very comprehensive article on web page optimization: http://developer.yahoo.com/performance/rules.html

Okay. It's about to officially start!

I. Merge file requests (using css files as an example to describe how to process JavaScript files)

We usually store css on the webpage in the css file and then introduce it into the webpage. when loading the webpage, the browser will generate a request for each file reference. The creation of a request consumes a certain amount of bandwidth resources and time and server resources. If the number of requests is too large, the loading speed of the webpage slows down. One way is to combine multiple css files into one file, which not only facilitates conflicts between styles, but also makes it difficult to maintain them in the future, and may cause useless style data loading. The following method processes css file requests through an intermediate layer to merge file requests without merging css files. (Monitoring Tool: HttpWatch)

Not optimized:

Default. aspx

 1 
2
3 <link rel="stylesheet" type="text/css" href="styles/style1.css"/>
4 <link rel="stylesheet" type="text/css" href="styles/style2.css"/>
5
6 <body>
7 <div class="div1"></div>
8 <div class="div2"></div>
9 </body>
10

Style1.css

1 .div1{
2 width:500px;
3 height:100px;
4 border:solid 1px red;
5 background:url(../images/back.gif)
6 }

Style2.css

1 .div2{
2 width:600px;
3 height:100px;
4 border:solid 1px black;
5 background-color:red
6 }

First page loading result:

Obviously, two requests are sent to obtain the css file. The following uses a request to obtain two css files.

After optimization:

Defalut. aspx

1 
2
3 <link rel="stylesheet" type="text/css" href="CssRequestHandler.aspx?p1=styles/style1.css&p2=styles/style2.css"/>
4
5 <body>
6 <div class="div1"></div>
7 <div class="div2"></div>
8 </body>
9
Among them, CssRequestHandler is the middle layer for processing css requests. The path of the css file to be requested is sent to the server as a parameter. Note: The parameter names here must be different. If the same parameter name is used, the intermediate layer processing code will be a little different.
CssRequestHandler.ashx

1 public void ProcessRequest (HttpContext context ){
2 context. Response. ContentType = "text/css ";
3 int pCount = context. Request. QueryString. Count; // number of parameters
4 string path = string. Empty; // file path
5
6 for (int I = 0; I <pCount; ++ I)
7 {
8 path = context. Request. PathMap (context. Request. applicationpha + "/" + context. Request. QueryString [I]);
9 TextReader tr = StreamReader (path );
10 context. Response. Write (tr. ReadToEnd () + "\ n ");
11 tr. Close ();
12 tr. Dispose ();
13}
14}

Style1.css

1 .div1{
2 width:500px;
3 height:100px;
4 border:solid 1px red;
5 background:url(images/back.gif)
6 }

Note: The image url in the background attribute is different from the previous one, because merging the requested css file in this way is like nesting the css style into the html file, therefore, the relative path must be the relative path of the html page file.

Style2.css

1 .div2{
2 width:600px;
3 height:100px;
4 border:solid 1px black;
5 background-color:red
6 }

First page loading result:

Compared with the unoptimized one, the amount of data sent and received is reduced. However, the Time is increased. It is inferred that the request is sent only when the image has to wait for the css style explanation, while the middle layer processing takes some Time and delays the image request, therefore, the request to complete the last image is delayed. Try not to load the image:

After optimization:

Not optimized:

It can be found that the optimized page loading speed is improved, regardless of the amount of data sent and received or the page request time.

 

Next article: webpage optimization Series 2: Cache static files and images (asp.net)

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.