Telerik UI for ASP.NET AJAX

What is RadCompression?

Simply put, RadCompression is a HttpModule that is shipped with the Telerik UI for ASP.NET AJAX and is designed to automatically compress your AJAX and Web Service responses. In other words, RadCompression will intercept the bits that your server is sending back to a browser (or Silverlight-client, for that matter) and compress them. Once the compressed bits reach the browser, standard browser technology takes over and decompresses the response, so your application can work with it normally. The compression process is completely transparent to your client-side code (JavaScript or Silverlight) and your server-side code. It simply reduces the number of bits that are sent over the wire (from your server to your client) and thus - in theory - improves your page performance by reducing the TTLB (time to last byte).

What RadCompression is not?

RadCompression is not designed to be a complete replacement for other HTTP compression tools, such as the built-in HTTP Compression in IIS 7. Instead, it is designed to work with those existing tools to cover scenarios they usually miss - namely the compression of bits moving back and forth in AJAX (and now Silverlight) applications. If you have HTTP Compression enabled in IIS7, you'll discover that it does not compress your AJAX and Web Service responses; it only compresses the initial bits sent to the browser when the page is requested. By adding RadCompression to your project, you cover those gaps and start compressing your XHR (XmlHttpRequest - i.e. the "X" in AJAX).

So, if RadCompression does not cover all HTTP traffic, what does it cover? Quite simply, RadCompression will automatically detect and compress requests that expect these content response types (as found in the HTTP request's "ContentType" header or "AcceptsTypes" header):

  • application/x-www-form-urlencoded

  • application/json

  • application/xml

  • application/atom+xml

These types generally reflect the content types returned by AJAX Web Services, ADO.NET Data Services, and AJAX UpdatePanel responses, though there certainly are other scenarios that return these content types (such as a typical RSS feed). If a browser supports compression (most modern browsers do) and RadCompression is enabled, all content of this type will be compressed. The one exception is IE6, which does not support this compression well, so RadCompression will automatically ignore requests coming from IE6 clients.

How is RadCompression enabled?

Enabling RadCompression could not be easier. It is a simple matter of adding a HttpModule registration to the site's web.config. Specifically, you need the following:

CopyWeb.config
<httpmodules>
    ...
    <!-- Add this line exactly as is - the name value is important -->
    <add name="RadCompression" type="Telerik.Web.UI.RadCompression" />
</httpmodules>
<!-- If you're using IIS7, then add this, too-->
<system.webserver>
 <modules>
   ...
   <add name="RadCompression" type="Telerik.Web.UI.RadCompression" />
 </modules>
...     
</system.webserver>

By default the RadCompression module will compress AJAX requests only (with the content type headers specified above), however you can enable compression for regular postbacks as well setting the enablePostbackCompression property to true (its default value is false, see web.config section below).You also have the option to disable the compression for particular pages if necessary. To do that use the RadCompressionSettings attribute of the page in question:

Another aspect of the RadCompression model allows you to exclude particular request handlers through the RadCompression web.config configuration settings. Here is an example how this can be done (note the matchExact attribute which determines whether the rule will be forced for the specified path only or globally for the entire web site/web application project):

Copyweb.config
<configSections>
....................
<sectionGroup name="telerik.web.ui">
      <section name="radCompression" type="Telerik.Web.UI.RadCompressionConfigurationSection, Telerik.Web.UI, PublicKeyToken=121fae78165ba3d4" allowDefinition="MachineToApplication" requirePermission="false"/>
</sectionGroup>
....................
</configSections>
<telerik.web.ui>  
    <radCompression>   
        <excludeHandlers>
            <!--This will match only the defaultcs.aspx file inside a grid folder in web site root-->
            <add handlerPath="grid/defaultcs.aspx" matchExact="true"/>
            <!--This will match every defaultvb.aspx file regardless of its location in the web site-->
            <add handlerPath="defaultvb.aspx" matchExact="false"/>  
            <!--This will match the handlers of all pages which reside in the MyFolder sub-folder of the web site-->  
            <add handlerPath="MyFolder/" matchExact="false"/>
        </excludeHandlers>
    </radCompression>     
</telerik.web.ui>
Note

When using IIS 7.5 dynamic compression enabling RadCompression is not required as this will result in double compression of the response.

ViewState compression

When you enable the RadCompression module, you get the entire response compressed including the ViewState. However, you also have the option to turn on only ViewState compression and store it either in a hidden field, or in the Session (to pass and retrieve it from there on form submits), without compressing the rest of the response. For this purpose you can use additional page adapters which override the default page adapter for viewstate storage. To enable viewstate compression you can register these RadCompression controlAdapters using BrowserFile.browser file in App_Browsers folder of your web site/project. Here is how to register a hidden field which will be used as a container for the compressed page viewstate:

CopyBrowserFile.browser
<browsers>  
 <browser refID="Default">   
   <controlAdapters>  
     <adapter controlType="System.Web.UI.Page" adapterType="Telerik.Web.UI.RadHiddenFieldPageStateCompression" />  
   </controlAdapters>  
 </browser>  
</browsers>

To use this adapter you do not need to enable RadCompression module through the web.config file as described above. RadHiddenFieldPageStateCompression might be used separately from the RadCompression module in cases where only viewstate compression is needed. Using both at the same time could result in compressing the ViewState twice.

You can also store the compressed ViewState in the Session:

CopyBrowserFile.browser
<browsers>  
 <browser refID="Default">   
   <controlAdapters>  
     <adapter controlType="System.Web.UI.Page" adapterType="Telerik.Web.UI.RadSessionPageStateCompression" />  
   </controlAdapters>  
 </browser>  
</browsers>

In this case enabling RadCompression at the same time would be fine, as when the compressed ViewState is saved in Session, it is not included in the response, so the RadCompression module does not compress it.

RadCompression and SessionPageState

Due to the fact that RadCompression module takes advantage of the ASP.NET's SessionPageStatePersister there are some scenarios where you may need to fine-tune its settings:

  • ControlState - by default, the SessionPageStatePersister doesn't add the ControlState to the Session so you may need to add it manually:

    CopyWeb.config
    <system.web>
        <browserCaps>
            <case>
                RequiresControlStateInSession=true
            </case>
        </browserCaps>
    </system.web>
  • Page history - in applications where you have a lot of popup windows, it is important to increase the amount of the pages that are persisted in the Session. The default value of the history size is 9.

    CopyWeb.config
    <system.web>   
      <sessionPageState historySize="15" />
    </system.web>
Note

When IIS 7.5 dynamic compression is enabled the ViewState will be automatically compressed even if the RadCompression module is not enabled.

How to enable compression for regular postbacks?

You can enable the postback compression by setting the enablePostbackCompression property of the RadCompression module to true (the default value is false). This can be done at application level in the following manner:

CopyWeb.config
<configSections>
....................
<sectionGroup name="telerik.web.ui">
      <section name="radCompression" type="Telerik.Web.UI.RadCompressionConfigurationSection, Telerik.Web.UI, PublicKeyToken=121fae78165ba3d4" allowDefinition="MachineToApplication" requirePermission="false"/>
</sectionGroup>
....................
</configSections>
<telerik.web.ui>  
   <radCompression enablePostbackCompression="true"/>
</telerik.web.ui>
Note

Note that setting the same attribute at page level will override the web.config settings because it will be treated with higher priority.

Known issues

Problem

When RadCompression is enabled and you are using .NET 4.0, event handlers might not be raised in a default document in IIS 7 or IIS 7.5 Integrated Mode.

Solution

This problem is caused by a breaking change in .NET 4.0 described here. To workaround it one can set preCondition="managedHandler" for the RadCompression module. You may also need to remove the runAllManagedModulesForAllRequests setting from your web.config if you have it (or set it to false).

What is the benefit?

You can see the results from eight unique tests made by our evangelist Todd Anglin in the following blog post and in this blog post concerning the usage of page adapters.

Optimization tip summary

When it comes to RadCompression, the impact it has on your site just depends on where your users are located. If you have a site that is deployed over the web where latency and connection speeds are unpredictable, reducing the bytes you send over the wire is an easy way to improve your site's performance. And since RadCompression can literally be implemented with a single change to your config file, you really do not have much to lose. In a quick word:

RadCompression is an easy way to reduce the bytes sent over the wire for XHR operations.