Wednesday, September 03, 2008

Serving up the MS AJAX Runtime as a CompositeScript

ASP.NET 3.5 SP1 introduces script combining to the ScriptManager.  To take advantage of this feature with your own scripts, you simply wrap  you <Scripts>...</Scripts> block with a <CompositeScript>...</CompositeScript> block.  This is great, but if you've spent any time analyzing your site with a HTTP tracing tool like Firebug, Web Development  Helper or Fiddler, you'll notice that the MS AJAX Runtimes (MicrosoftAjax.js, MicrosoftAjaxWebForms.js) is still served up separately.  It turns out that getting these to be served as part of the combined script is relatively straight forward and entails manually including the scripts within your <CompositeScript> block.  Since the names are unique, the ScriptManager will not re-add the runtime libraries.  The final result looks like:

<asp:ScriptManager runat="server" ID="scriptManager">
    <CompositeScript>
        <Scripts>
            <asp:ScriptReference Name="MicrosoftAjax.js" />
            <asp:ScriptReference Name="MicrosoftAjaxWebForms.js" />
            <asp:ScriptReference Path="~/scripts/YourScript.js" />
        </Scripts>
    </CompositeScript>
</asp:ScriptManager>