Click here to Skip to main content
Licence CPOL
First Posted 14 May 2010
Views 5,845
Bookmarked 0 times

Using the Microsoft Ajax Library 3.5 with the CDN

By | 14 May 2010 | Technical Blog
Using the Microsoft Ajax Library 3.5 with the CDN
A Technical Blog article. View original blog here.[^]

Recently Microsoft has announced its Microsoft Ajax content delivery network (CDN) which can significantly improve the performance of any ASP.NET AJAX web application. When it was first announced, the CDN was almost useless for the current web applications based on ASP.NET 3.5 since it did not host the Microsoft Ajax library 3.5 and did not support content delivery via SSL. However in a very short period of time Microsoft was able to fix those problems (good job!) and now web applications built on ASP.NET 3.5 can benefit from using Microsoft Ajax CDN.

So how do you make MicrosoftAjax.js file being referenced from the CDN as opposed to the embedded file? Quite easy actually with the help of the ScriptManager control. Add the following declaration to a page (or Master page) where there is a ScriptManager control:

<script type="syntaxhighlighter" class="brush: xhtml"> 
<asp:scriptmanager runat="server" enablepartialrendering="false"> 
<scripts> 
<asp:scriptreference name="MicrosoftAjax.js" 
path="http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js" /> 
</scripts> 
</script>

What that declaration means is that the script with a name MicrosoftAjax.js which is always automatically referenced by a ScriptManager from the System.Web.Extensions DLL now should be referenced from that location: http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js.

This is how it is referenced by default:

<script src="http://www.codeproject.com/ScriptResource.axd?d=
eYUqBJhfSVL41hIDYkBL0tfaps9hoQId_48PydfbcyWH41vNvL68sk-l7P9FLAPz7b4vtI8WkZ-
ezAF0b_ZkyG52wt9oUtaQ5ezFfGBr7LY1&t=ffffffffef976216" type="text/javascript"></script>

and after we include the new ScriptReference declaration:

<script src=http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js 
type="text/javascript"></script>

ScriptManager is even smart enough to automatically reference the debug version MicrosoftAjax.debug.js from the CDN when debugging is enabled in the web.config file:

<compilation debug="true">

<script src=http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.debug.js 
type="text/javascript"></script>

So this is clear: we have our MicrosoftAjax.js referenced from the CDN and that improves our web application performance and saves us and the visitors some bandwidth since an internet browser will reuse the same cached copy of the MicrosoftAjax.js from the CDN for different web applications that reference it.

Using the CDN Conditionally

What if we only wanted to reference MicrosoftAjax.js from the CDN when the web application is deployed to a production environment and use an embedded version in a development environment? That would make sense in order for the developers to work without having to be connected to the Internet. Once again, it can be done but this time we'll need to write some code. We are going to add the MicrosoftAjax.js script reference dynamically depending on the debug value in the web.config file; we only add the script reference when debug="false":

<script type="syntaxhighlighter" class="brush: csharp"> 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Context.IsDebuggingEnabled) 
    { 
        ScriptManager sm = ScriptManager.GetCurrent(this); 
        sm.Scripts.Add(new ScriptReference 
        {
	   Name = "MicrosoftAjax.js", 
	   Path = http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js}); 
        } 
    } 
</script>

Using the CDN via SSL

Another major case is when our web application has pages that are served via SSL. In this case, we want to automatically select the correct CDN URL for the MicrosoftAjax.js. In order to do that, we just modify the previous code:

<script type="syntaxhighlighter" class="brush: csharp"> 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Context.IsDebuggingEnabled) 
    { 
        ScriptManager sm = ScriptManager.GetCurrent(this); 
        sm.Scripts.Add(new ScriptReference 
        {
            Name = "MicrosoftAjax.js", Path = Request.Url.Scheme + 
		"://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js"}); 
        } 
    } 
</script>

So that's it for now. Enjoy using the Microsoft Ajax CDN.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alexander Turlov

Architect

Canada Canada

Member

Follow on Twitter Follow on Twitter
Alexander Turlov has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, and Basic, C, C++ and C#. He's been working for different industries like science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of interests is rich web applications development with .NET, C#, ASP.NET and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft products such as Visual Studio, SQL Server, P&P Software Factories, Enterprise Library and some other products like BizTalk and SharePoint. He holds a M.Sc. degree in physics and an MCSD.NET certification.

View my profile on LinkedIn

View my blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 14 May 2010
Article Copyright 2010 by Alexander Turlov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid