Static resources are called static resources. Cache these static resources to the client to reduce the number of requests when the user browses the web page again, thus accelerating the loading and rendering of the web page. Similarly, to set static resource cache to the client, we need to add an intermediate layer to process static resource requests. The following uses an image as an example. (This method is used only when the image is very large. If the image is k-series, the initial loading speed will be slower, because IIS processes static files and dynamic files differently, if the image size is small, dynamic file processing takes up most of the total loading time)
Not optimized:
Default. aspx
1 2 3 4 <body>
5
6 <body>
7
For the first and second access to the page, you must request images from the server.
After optimization:
Default. aspx:
1 2 3 4 <body>
5
6 <body>
7
ImageRequestHandler. ashx
1 public void ProcessRequest (HttpContext context ){
2 string path = context. Server. MapPath (context. Request. QueryString [0]);
3 string suffix = path. Split ('.') [path. Split ('.'). Length-1];
4 context. Response. ContentType = string. Format ("image/{0}", suffix. ToLower (). Equals ("png ")? "X-png": suffix); // sets MIME. If it is a png file, the MIME information is text/x-png.
5 context. Response. Expires = 60*24*30; // set the image to expire in 30 days.
6 ImageFormat ift = ImageFormat. Jpeg; // set the default file format
7 Image img = Image. FromFile (path );
8 if (suffix. ToLower (). Equals ("gif "))
9 {
10 ift = ImageFormat. Gif;
11} else if (suffix. ToLower (). Equals ("png "))
12 {
13 ift = ImageFormat. Png;
14}
15 MemoryStream MS = new MemoryStream ();
16 img. Save (MS, ift );
17 context. Response. OutputStream. Write (ms. GetBuffer (), 0, ms. Length );
ms.Close();
ms.Dispose();
img.Dispose();
18 }
Only image files in gif, Jpeg, and png formats are processed here. You can add and modify image files in other formats as needed. If there is no png file in the processing process, the code of Lines 15 to 17 can be rewritten
img.Save(context.Response.OutputStream,ift);
Why? This is because the stream Response. OutputStream cannot be read back, that is, its CanSeek attribute is false. When a png image is generated, it is not like jpeg. It is not streaming. If it has already been written, it is no longer necessary. Instead, it is necessary to constantly write structural data back. However, the response stream cannot go back to seek, so it cannot be used directly. Change it to a MemoryStream that can be seek. convert it into a png image and then output it to the response stream. Otherwise, a general error of GDI + occurs.
The number of requests is the same, but the image is obtained from the cache, and no 304 error is returned after the server is requested.
The following is a summary of the operations that trigger reading existing files in the cache after the file is cached to the cache.Premise: the file has not expired
1. Enter the address in the address bar of the browser and press jump;
2. Click the hyperlink on the page;
3. Events that trigger server controls;
4. The script code uses window. open (), location. href = '', location. assign (), location. replace ().
The following operations do not read the existing files in the cache even if the files have not expired.
1. Press the refresh button of the browser;
2. The script code uses location. reload ().
The browser's back and forward buttons are irrelevant to whether the file is cached. They call files in the history cache.
More: webpage optimization Series 3: Use Compressed Post viewstate