Welcome to MSDN Blogs Sign in | Join | Help

Script combining made better [Overview of improvements to the AJAX Control Toolkit's ToolkitScriptManager]

The 10606 release of the AJAX Control Toolkit introduced ToolkitScriptManager, a new class that extends the ASP.NET AJAX ScriptManager to perform automatic script combining. I blogged an overview of ToolkitScriptManager last week (including an explanation of what "script combining" is). This post will build on that overview to discuss some of the changes to ToolkitScriptManager in the 10618 release of the Toolkit.

The most obvious change is the addition of an optional new property: CombineScriptsHandlerUrl. Left unset, ToolkitScriptManager works just like before; setting CombineScriptsHandlerUrl specifies the URL of an IHttpHandler (feel free to use @ WebHandler to implement it) that's used to serve the combined script files for that ToolkitScriptManager instance instead of piggybacking them on the host page itself. Implementing this handler is simple: just use the CombineScriptsHandler.ashx file that's part of the SampleWebSite that comes with the Toolkit! :) If you look at how that handler works, the ProcessRequest method simply calls through to the ToolkitScriptManager.OutputCombinedScriptFile(HttpContext context) method which is public and static for exactly this purpose. OutputCombinedScriptFile does all the work of generating the combined script file and outputting it via the supplied HttpContext - in fact, this is the same method that ToolkitScriptManager now uses internally to output a piggybacked combined script file. Because adding a handler in this manner doesn't require modifying the server configuration, CombineScriptsHandlerUrl can also be used by people in hosted and/or partial trust scenarios.

At the end of my previous post, I mentioned two tradeoffs that were part of switching from ScriptManager to ToolkitScriptManager. Both of those tradeoffs are addressed by the 10618 ToolkitScriptManager - plus one more I didn't know about then and another that's implicit:

  • Using CombineScriptsHandlerUrl incurs no unnecessary load on the server. One of the tradeoffs of the piggyback method for generating combined script files is that it involves a little bit of extra processing as part of the host page's page lifecycle that's not strictly necessary for the purposes of generating the combined script file. ToolkitScriptManager manages the page lifecycle processing to minimize the impact of piggybacking, but can't eliminate it all. However, using CombineScriptsHandlerUrl with a dedicated IHttpHandler doesn't involve any such overhead and helps keep things as efficient and streamlined as possible. The host page doesn't get reinvoked and the dedicated IHttpHandler does no more than it needs to.
  • Using CombineScriptsHandlerUrl won't interfere with URL rewriting. Customers using URL rewriting with their web sites pointed out that the piggybacking approach to combined script generation might require them to revise their URL rewriting rules to account for the unexpected page requests with the special combined script request parameter. ToolkitScriptManager tries to be as easy to use as possible, so one of the nice things about setting the new CombineScriptsHandlerUrl property is that web site authors can choose whatever URL works best for them to be their combined script file handler, thereby avoiding conflict with existing URL rewriting rules.
  • Using CombineScriptsHandlerUrl makes it more likely that cached script files will be reused. When piggybacking combined script URLs, the presence of the host page's base URL in the combined script URL means that any cached script files generated by page A will not be usable by page B (which has a different base URL). Of course, once the user's browser caches the combined script files for pages A and B, the cached versions will be used and there is no server impact - but page B won't benefit from page A's cached file even if the combined script files are otherwise identical. When pages A and B both use the same set of extenders and CombineScriptsHandlerUrl is specified, the combined script file URL generated by pages A and B will be identical (because the combined script file handler base URL will be the same for both) and therefore the combined script file cached by the user's browser for page A will be automatically used for page B as well. For web sites with common extender usage patterns (such as a TextBoxWatermark'd search box in the corner of every page), the caching benefits of CombineScriptsHandlerUrl could be significant.
  • The URLs used to specify combined script files are considerably less verbose. Rather than including the full script name for every script in the combined script file (often upwards of 20 or 30 characters), the hexadecimal representation of their String.GetHashCode is used instead (8 or fewer characters). While the baseline combined script URL length has grown by a bit due to some other changes, by far the most significant source of URL length was the script names, so the new URLs are shorter and less likely to get long (even on pages with lots of extenders). This improvement applies whether CombineScriptsHandlerUrl is specified or not, so piggybacked URLs are shorter, too. Note: Because hash code collision is now possible (though extremely unlikely), there's a bit of code to detect that scenario and throw an informative exception. (Just change either of the script names slightly to resolve the collision.)

A handful of other fixes and improvements were made to ToolkitScriptManager for the 10618 release. Notably:

  • The CurrentUICulture is now embedded in the combined script URL so that changing the browser's culture while viewing a site will properly update the culture of the site's extenders.
  • ToolkitScriptManager's check for a script's eligibility to participate in script combining now includes a check for the WebResource attribute which is one of the things that ASP.NET's ScriptResourceHandler requires in order to serve an embedded resource. Consequently, an assembly's embedded resources without a corresponding WebResource attribute are no longer eligible for script combining (without needing to use the ExcludeScripts to explicitly remove them). This makes ToolkitScriptManager's behavior more consistent with that of ScriptResourceHandler.
  • The "magic" request parameter for the combined script file changed from "_scriptcombiner_" to "_TSM_HiddenField_"/"_TSM_CombinedScripts_". _TSM_CombinedScripts_ is simply a rename of _scriptcombiner_, while _TSM_HiddenField_ now specifies the HiddenField that's used for tracking which scripts have already been loaded by the browser. This ID is implicitly available when piggybacking, but is not available in the CombineScriptsHandlerUrl case, so it has become part of the URL. For completeness, the new form of the combined script URL is now:
    .../[Page.aspx|Handler.ashx]?_TSM_HiddenField_=HiddenFieldID&_TSM_CombinedScripts_=;Assembly1.dll Version=1:Culture:MVID1:ScriptName1Hash:ScriptName2Hash;Assembly2.dll Version=2:Culture:MVID2:ScriptName3Hash

If you're already using ToolkitScriptManager and want to start using CombineScriptsHandlerUrl, but don't want to have to modify a bunch of ASPX pages to add the new property, you can take advantage of the fact that ToolkitScriptManager is now decorated with the Themeable attribute and can be customized by a .skin file as part of ASP.NET's Theme/Skin support. Adding CombineScriptsHandlerUrl to all the pages of the Toolkit's SampleWebSite was easy - I just added a ToolkitScriptManager.skin file to the existing web site theme and used the code <ajaxToolkit:ToolkitScriptManager runat="server" CombineScriptsHandlerUrl="~/CombineScriptsHandler.ashx" /> to set CombineScriptsHandlerUrl for the entire site.

ToolkitScriptManager is a handy way to enhance a web site with the AJAX Control Toolkit and it's gotten even better with the 10618 release of the Toolkit. We think ToolkitScriptManager offers some pretty compelling enhancements and we use it for all the AJAX Control Toolkit's sample content. We encourage anyone who's interested to give ToolkitScriptManager a try and see how well it works for them. As always, if there are any problems, please let us know by posting a detailed description of the problem to the AJAX Control Toolkit support forum.

Happy script combining!!

Published Wednesday, June 20, 2007 3:14 PM by Delay

Comments

# There's a new release of the AJAX toolkit

Thursday, June 21, 2007 4:40 AM by LA.NET [EN]

And it seems like it solves some of the issues i&#39;ve mentioned before and brings improves to other

# re: Script combining made better [Overview of improvements to the AJAX Control Toolkit's ToolkitScriptManager]

Saturday, June 23, 2007 1:38 PM by Fabrice Marguerie

Thank you! Much better solution.

Thank you for providing script combining altogether.

# Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles

Tuesday, June 26, 2007 2:52 AM by ScottGu's Blog

Last week the ASP.NET AJAX Control Toolkit team released Build 10618 of the ASP.NET AJAX Control Toolkit

# Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles

Tuesday, June 26, 2007 2:56 AM by ASP.NET

Last week the ASP.NET AJAX Control Toolkit team released Build 10618 of the ASP.NET AJAX Control Toolkit.

# Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles

Tuesday, June 26, 2007 3:11 AM by BusinessRx Reading List

Last week the ASP.NET AJAX Control Toolkit team released Build 10618 of the ASP.NET AJAX Control Toolkit.

# download New Ajax toolskit and 5 new How to

Friday, June 29, 2007 12:37 AM by links

http://weblogs.asp.net/scottgu/archive/2007/06/25/updated-asp-net-ajax-control-toolkit-release-and-new-asp-net-ajax-videos-articles.aspx Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles Last week the ASP.NET AJAX Control

# Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles

Wednesday, July 25, 2007 3:52 AM by 拾荒时代

浏览器 UpdatedASP.NETAJAXControlToolkitReleaseandNewASP.NETAJAXVideos/Articles ...

# Speed up load time of AJAX Control Toolkit controls while debugging

Friday, February 15, 2008 3:27 PM by Loren Halvorson's Blog

This is probably already well known and well documented, but I still occasionally see people bemoaning

# Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles

Wednesday, February 27, 2008 11:32 AM by rox19840702

Updated ASP.NET AJAX Control Toolkit Release and New ASP.NET AJAX Videos/Articles

# Script Combining Issues when upgrading

Friday, December 19, 2008 8:04 AM by GilesHinton

Hi,

I've recently upgraded from Version 1.0.10618.0 to the latest ASP.Net 2.0 version of the toolkit (Version 1.2.0229 if I've got it right!).

With the previous version of the toolkit, everything worked perfectly, and my scripts were combined properly.

Unfortunately, with the latest version, I've noticed that whilst the initial load of the page combines the script files properly, following any AJAX asynchronous postback, I get a Sys.ScriptLoadFailedException JavaScript exception saying the ScriptManager can't load the script:

e.g. /MyApp/Page.aspx?

_TSM_HiddenField_=sm_HiddenField&amp;_TSM_CombinedScripts_=%3b%3bWeb%3aen-US%3acfbff61f-f484-45e0-8c5b-07edc64cb9ab%3a8da9c810%3bAjaxControlToolkit%2c+Version%3d1.0.20229.16575%2c+Culture%3dneutral%2c+PublicKeyToken%3d28f01b0e84b6d53e%3aen-US%3aedadbfc1-89e6-4fc6-9f7b-7f2306bf5c53%3a2d0cbeda' could not be loaded

Any ideas what could be causing this?

I'm referencing the AjaxControlToolkit project directly, but have been unable so far to ascertain exactly where it's tripping up - I'm going to take a look at the client side exception shortly to see if that sheds any light on this...

Hope you can help.

# Script Combining Issues when upgrading

Friday, December 19, 2008 11:23 AM by GilesHinton

Please discard the last comment - it would appear this is a known issue with Script Combining with the latest version of the toolkit that was reported on codeplex in Oct 2007.

http://www.codeplex.com/AjaxControlToolkit/WorkItem/View.aspx?WorkItemId=13134

Surprised this hasn't been addressed yet...

# re: Script combining made better [Overview of improvements to the AJAX Control Toolkit's ToolkitScriptManager]

Friday, December 19, 2008 6:33 PM by Delay

Giles,

Sorry for the trouble! I'll send an email in a moment to make sure the relevant folks know about the issue. At this point, Bertrand Le Roy (http://weblogs.asp.net/bleroy/) is probably your best contact if you need to follow up further. Thanks very much for your patience and help here!

# ToolkitScriptManager.CombineScriptsHandlerUrl

Sunday, January 11, 2009 12:53 PM by TristanIce

Hi,

I have a small question about ToolkitScriptManager.CombineScriptsHandlerUrl.

I used this 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.

# re: Script combining made better [Overview of improvements to the AJAX Control Toolkit's ToolkitScriptManager]

Monday, January 12, 2009 3:35 PM by Delay

TristanIce,

Sorry for the trouble! Unfortunately, my knowledge of the latest AJAX Control Toolkit releases is out of date. Bertrand (see my comment/pointer above) is probably your best bet for this question.

Hope this helps!

Anonymous comments are disabled
 
Page view tracker