April 18, 2008 3

Three quick ways optimize AJAX driven websites in ASP.NET

By in Tags: ,

Recently I was involved in a project where I had to make heavy use of AJAX. I realized there are a few simple things you could do to improve performance.

1) Combine scripts

<ajaxToolkit:ToolkitScriptManager ID="TSM1" runat="Server"
EnablePartialRendering="true"
CombineScriptsHandlerUrl="~/CombineScriptsHandler.ashx" />

As the name of the property suggests, it will pretty much combine all the needed JS files into one which in turn will reduce the number of requests sent to the server. You can find a detailed discussion about this here.

It is pretty easy to implement; instead of using the regular ScriptManager, just switch to the ToolkitScriptManager which comes with the AjaxToolkit and then set its CombineScriptsHandlerUrl property as shown above and throw the CombineScriptsHandler.ashx (included in the “SampleWebSite” directory of AjaxControlToolkit’s release package) into the root.

2) Run in release mode

The debug versions of the AJAX library have their source formatting preserved, as well as some debug asserts. By running it in release mode you can shave off some bytes off your requests.

<ajaxToolkit:ToolkitScriptManager ID="TSM1" runat="Server" 
EnablePartialRendering="true" ScriptMode="Release" />

Although, its important to note that some versions of Safari don’t seem to be compatible with this and could cause many strange side effects as this person and I have experienced in the past.

On a side note, ASP.NET AJAX Control Toolkit officially does not support Macs with PowerPC processors, its good to know that piece of information if a client ever demands an explanation as for why AJAX powered functionality seems to be broken or not functioning as expected in that environment.

3) Enable script caching and compression in web.config


<system.web.extensions>
  <scripting>
    <scriptResourceHandler enableCompression="true"
     enableCaching="true"/>
  </scripting>
</system.web.extensions>

This will compress and cache all the script files which are embedded as resources in an assembly, localization objects, and scripts that are served by the script resource handler.

But like the previous tip, there is a exception to this one too. Some versions of IE6 have a bug where they cant’t handle GZIP’d script files correctly. The RTM version of ASP.NET AJAX works around this by explicitly not compressing files for these versions of IE. Although if you are still having a problem, it just might be a safe bet to explicitly set the enableCompression property to false in the web.config.

Tags: ,

3 Responses to “Three quick ways optimize AJAX driven websites in ASP.NET”

  1. Bina says:

    Hey! Yes def. going to be a techie blog :-)

  2. Daniel says:

    Good list!

    I might add “combining ASP.NET AJAX script method proxies”, and, one wide open question on optimization using the AJAXControlToolkit might be how to automatically remove comments / minify the javascript in the toolkit. Even with the ToolkitScriptsManager in Release mode, comments and whitespace are preserved in script delivered to the browser.

  3. Igor says:

    Hi,

    I have a small question about ToolkitScriptManager.CombineScriptsHandlerUrl.

    I used a property ToolkitScriptManager.CombineScriptsHandlerUrl to point to CombineScriptsHandler.ashx from sample web site provided with toolkit. The problem is that it wont cache, the handler always returns http status 200 OK.

    What am I missing?

    Thanks.

Leave a Reply