Preface
This article is my Practice of Improving the loading speed of ASP. NET pages. These practices are divided into the following parts:
- 1. Use the HTTP module to control the page lifecycle.
- 2. Customize response. Filter to get the static content (disk cache) of the dynamic page generated by the output stream ).
- 3. gzip compression on the page.
- 4. outputcache program outputs the page cache.
- 5. Delete blank strings on the page. (Similar to Google)
- 6. Completely Delete the viewstate.
- 7. Delete the junk namingcontainer generated by the server control.
- 8. Use scheduled tasks to generate pages on time. (This article does not include the implementation of this practice)
- 9. JS, CSS compression, merging, caching, and image caching. (This article does not include the implementation of this practice)
- 10. cache corruption. (Excluding implementation of 9th practices)
In response to the above practices, we first need an HTTP module, which is the portal and core of the entire page process.
1. Customize response. Filter to get the static content of the dynamic page generated by the output stream (disk cache)
The following code shows that we use request. rawurl is the basis for caching, because it can contain any querystring variable. Then, we use MD5 encryption to obtain the variable of the local file name on the server, and instantiate a fileinfo to operate the file, if the last file generation time is less than 7 days, we use. the transmitfile method added by net2.0 sends the static content of the stored file to the browser. If the file does not exist, we will pass the Stream Obtained by response. filter to the commonfilter class and use filestream to write the dynamic page content to the static file.
namespace ASPNET_CL.Code.HttpModules { public class CommonModule : IHttpModule { public void Init( HttpApplication application ) { application.BeginRequest += Application_BeginRequest; } private void Application_BeginRequest( object sender, EventArgs e ) { var context = HttpContext.Current; var request = context.Request; var url = request.RawUrl; var response = context.Response; var path = GetPath( url ); var file = new FileInfo( path ); if ( DateTime.Now.Subtract( file.LastWriteTime ).TotalDays < 7 ) { response.TransmitFile( path ); response.End(); return; } try { var stream = file.OpenWrite(); response.Filter = new CommonFilter( response.Filter, stream ); } catch ( Exception ) { //Log.Insert(""); } } public void Dispose() { } private static string GetPath( string url ) { var hash = Hash( url ); string fold = HttpContext.Current.Server.MapPath( "~/Temp/" ); return string.Concat( fold, hash ); } private static string Hash( string url ) { url = url.ToUpperInvariant(); var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); var bs = md5.ComputeHash( Encoding.ASCII.GetBytes( url ) ); var s = new StringBuilder(); foreach ( var b in bs ) { s.Append( b.ToString( "x2" ).ToLower() ); } return s.ToString(); } }}
Ii. Page gzip Compression
Gzip compression on pages is one of several major practices for explaining high-performance web programs in almost every article, because gzip compression can reduce the number of bytes sent by the server, this allows customers to feel that the webpage speed is faster and bandwidth usage is reduced. Of course, the client browser also supports it. Therefore, if the client supports gzip, we will send the content compressed by gzip. If not, we will directly send the content of the static file. Fortunately, modern browsers such as ie6.7.8.0 and Firefox support gzip.
To implement this function, we need to rewrite the above application_beginrequest event:
Private void application_beginrequest (Object sender, eventargs e) {var context = httpcontext. current; var request = context. request; var url = request. rawurl; var response = context. response; var Path = getpath (URL); var file = new fileinfo (PATH); // use page compression responsecompressiontype compressiontype = This. getcompressionmode (request); If (compressiontype! = Responsecompressiontype. none) {response. appendheader ("content-encoding", compressiontype. tostring (). tolower (); If (compressiontype = responsecompressiontype. gzip) {response. filter = new gzipstream (response. filter, compressionmode. compress);} else {response. filter = new deflatestream (response. filter, compressionmode. compress) ;}} if (datetime. now. subtract (file. lastwritetime ). totalminutes <5) {response. transmitfile (PATH); response. end (); return;} Try {var stream = file. openwrite (); response. filter = new commonfilter (response. filter, stream);} catch (exception) {// log. insert ("") ;}} private responsecompressiontype getcompressionmode (httprequest request) {string acceptencoding = request. headers ["Accept-encoding"]; If (string. isnullorempty (acceptencoding) return responsecompressiontype. none; acceptencoding = acceptencoding. toupperinvariant (); If (acceptencoding. contains ("gzip") return responsecompressiontype. gzip; else if (acceptencoding. contains ("deflate") return responsecompressiontype. deflate; else return responsecompressiontype. none;} private Enum responsecompressiontype {none, Gzip, deflate}
Iii. outputcache Programming Method output page Cache
The embedded outputcache cache of ASP. NET can cache content in three places: Web server, proxy server, and browser. ASP. net After msil, first write the result to the output cache, and then send it to the browser. When the user accesses the page in the same path, Asp. net will directly send the cached content without passing through. the process of aspx compilation and execution of msil. Therefore, although the program's own efficiency is not improved, the page loading speed is improved.
To implement this function, we continue to rewrite the above application_beginrequest event. After transmitfilePathThe page is cached as outputcache programming:
Private void application_beginrequest (Object sender, eventargs e ){//............. if (datetime. now. subtract (file. lastwritetime ). totalminutes <5) {response. transmitfile (PATH); // Add the outputcache cache header and cache it in the client response. cache. setexpires (datetime. now. addminutes (5); response. cache. setcacheability (httpcacheability. public); response. end (); return ;}//............}
Original translated from blog Park: http://www.cnblogs.com/cnshangsha/
More...