After my previous post I decided to spend some time researching the various ways to optimize ASP.NET applications. I found some interesting articles.HTTP Compression Module:I found this article on CodeProject. Compressing files before they are sent to the browser has many benefits. The page will load faster and less bandwidth is used, resulting in better user experience and reduced cost. There are a few different HTTP Compression module implementations out there, but this one adds to those by compressing WebResource.axd.This implementation has three parts to it. One is the compression of Web pages. It uses a custom filter to replace JavaScript and stylesheet links. To use the code is pretty easy. Once you've compiled the HTTPCompress module, you can add a reference to any .NET 2.0 site. Next, you should set a few parameters in the web.config and you're good to go. If you want to see whether the content is really filtered, you will need something that logs the HTTP pipeline.In the module, you need to hook up an event to the PostReleaseRequeststate. Before, I used to hook it up to the BeginRequest event but this executes way too early for the module to execute. At PostReleaseRequestState, the whole page has executed and its response content has been generated. 41 public void Init(HttpApplication context)
42 { 43 context.PostReleaseRequestState+= new EventHandler(context_PostReleaseRequestState); 44 }Next is combining multiple JavaScript and CSS files into one file and compressing them. It is better to have one JavaScript file and one CSS file for the browser to download. This is because the browser can't use parallel downloading for these types of files. Each one must be downloaded before another can begin. But it is also difficult to maintain large JavaScript and CSS files. The solution is to keep them separate, and then combine them into one file dynamically. And then on top of that, compress them. The HttpModule was updated to parse through the HTML of the page and replace the links to JavaScript and CSS files, combining wherever possible. It is now possible to write your pages like you normally would (in the past, to compress a script you would have to link to js.axd?files=jsfiletocompress.js) and the HttpModule will replace the link.Imagine, instead of including five JavaScript files equaling 177 KB, you can include one file equaling 52 KB. For example, in the sample Web site, there are 5 JavaScript files: 2 ScriptResource.axd, 2 WebResource.axd, and 1 *.js file that combines 5 *.js files in the JavaScript folder. Uncompressed, these files would equal 329 KB, but compressed they equal 89 KB. Those are huge savings.The third is the compression of WebResource.axd. All files are cached on the server and on the browser (including WebResource.axd). This way also enables you to include JavaScript or CSS from another domain and still compress and cache the files.This method also supports MS Ajax UpdatePanels.Using the code:Add a reference to the DLL or drag and drop the DLL into the bin folder of the Web application. Add the following to the web.config. It should work transparently, meaning you can develop your site without the compression module, then add the compression later; or develop with the compression module and then remove it later without having to make any changes besides deleting the web.config items and the DLL.<configSections>
<sectionGroup name="DCWeb"> <section name="HttpCompress" type="DC.Web.HttpCompress.Configuration, DC.Web.HttpCompress"/> </sectionGroup> </configSections> <DCWeb> <HttpCompress compressionType="GZip"> <IncludedMimeTypes> <add mime="text/html" /> </IncludedMimeTypes> <!--<ExcludedMimeTypes> <add mime="text/html" /> </ExcludedMimeTypes>--> <ExcludedPaths> <add path="~/NoCompress/Default.aspx" /> </ExcludedPaths> </HttpCompress> </DCWeb> <!-- The js.axd and css.axd must be enabled to allow javascript and css compression --> <httpHandlers> <add verb="*" path="js.axd,css.axd" type="DC.Web.HttpCompress.CompressionHandler,DC.Web.HttpCompress"/> </httpHandlers> <!-- The compression module must be enabled for the WebResource.axd to be compressed --> <httpModules> <add name="HttpCompressModule" type="DC.Web.HttpCompress.HttpModule,DC.Web.HttpCompress"/> </httpModules>