How to use Bundling and CDN
The ASP.NET AJAX Control Toolkit has many script and style files that it relies on. This can become a problem because each request takes time to be processed. And most browsers limit the number of simultaneous connections per hostname.
Bundling allows you solve this problem by combing multiple scripts or stylesheets into a single one and thus reduces the number of requests and improves the overall loading time.
With v15.1 release of the ASP.NET AJAX Control Toolkit, we have leveraged the ASP.NET approach to bundling by using the
ASP.NET Web Optimization Framework
. This framework builds bundles of web application content files located within the project.
How to enable bundling
NuGet package. You can install this by right-clicking the project in Solution Explorer and choosing “Manage NuGet packages…” and searching for and installing “AjaxControlToolkit.StaticResources
Installation of this nuget package makes the following changes to your application:
Manual changes required:
- All required scripts, styles, and images are added to the project (~/Scripts/AjaxControlToolkit and ~/Content/AjaxControlToolkit folders)
- AjaxControlToolkit config section is added to Web.config
- Script and style bundles are registered in the BundleTable.
After installation, please make the following manual changes to your project (these changes are usually made on the master page):
Add a ScriptReference to ScriptManager to register the script bundle:
Add the Styles.Render expression to the <head> element.
<asp:ScriptReference Path="~/Scripts/AjaxControlToolkit/Bundle" />
Switch compilation to the release mode in Web.config
<compilation debug="false" ... />
Once bundling is configured then you can verify that it works using the following step:
Open the browser and view the page source code: two links to ASP.NET AJAX Control Toolkit bundles should be rendered instead of links to individual files:
<link href="/Content/AjaxControlToolkit/Styles/Bundle?v=hash" rel="stylesheet"/>
To disable bundling:
- remove ScriptReference from ScriptManager
- remove the Style.Render expression from the <head> element
- in the “ajaxControlToolkit” config section (Web.config), set the “renderStyleLinks” attribute to “true”, and the “useStaticResources” attribute to “false”
ASP.NET AJAX Control Toolkit resource files (scripts, styles and images) are also available via Microsoft Ajax Content Delivery Network. The CDN provides better request handling and caching.
Please note that using CDN is not recommended on an Intranet and during development, since servers are closer to you than CDN servers in these cases.
Enabling CDN depends on whether bundling is used or not.
When bundling is turned on
, set the System.Web.Optimization.BundleTable.Bundles.UseCdn
property to True. This is commonly done in the Application_Start() method in the Global.asax file.
When bundling is turned off
, set the ScriptManager EnableCdn attribute to “true”:
<asp:ScriptManager runat="server" EnableCdn="true">