Using Microsoft Ajax Library 3.5 with CDN
Microsoft recently announced the Microsoft Ajax content delivery network (CDN), which can significantly increase the performance of any web application using ASP.NET AJAX. When this technology was announced, the CDN was almost useless for existing ASP.NET 3.5-based web applications, since the CDN did not contain the Microsoft Ajax library 3.5 and did not support SSL work. However, after a short period of time, Microoft was able to eliminate these problems and now web applications built on the basis of ASP.NET 3.5 can also use Microsoft Ajax CDN.
So how can you force a link to MicrosoftAjax.js to refer to a CDN instead of using the file stored in the DLL resources on your server?
Actually very simple with the help of ScriptManager. Add the following definition on the Page (or Master page) which has ScriptManager control:
<asp:ScriptManager runat= "server" EnablePartialRendering= "false" >
<scripts>
<asp:ScriptReference Name= "MicrosoftAjax.js"
Path= "http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js" />
</scripts>
</asp:ScriptManager>
')
This declaration means that a script named MicrosoftAjax.js, which is always automatically referenced by the ScriptManager, downloading it from the System.Web.Extensions dll should now be downloaded from this location:
ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js .
This link will be used by default:
<script src= "/ScriptResource.axd?d=eYUqBJhfSVL41hIDYkBL0tfaps9hoQId_
48PydfbcyWH41vNvL68sk-l7P9FLAPz7b4vtI8WkZ-ezAF0b_ZkyG52wt9oU
taQ5ezFfGBr7LY1&t=ffffffffef976216"
type= "text/javascript" >
</script>
and this is how it will look like after we add the definitions described above to the ScriptReference:
<script src= "http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js"
type= "text/javascript" >
</script>
Moreover, the ScriptManager is even smart enough to automatically use the debug version of the MicrosoftAjax.debug.js script from the CDN if the debug mode 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, everything is clear: we use MicrosoftAjax.js from CDN and this improves the performance of our web application and saves us and our visitors some traffic as the browser will reuse the same cached copy of MicrosoftAjax.js from CDN for all web applications, which link to it.
Using conditional script loading from CDN
What if we want to use MicrosoftAjax.js from CDN only when the web application was published to the production server and use the built-in script when we develop the site? This may make sense when programmers are developing without having an Internet connection. This option is also possible, but in this case we need to write some code. We are going to add a link to MicrosoftAjax.js dynamically, depending on the value of the debug variable in the file web.config; we add the necessary link only for the case when debug = "false":
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" });
}
}
Using CDN over SSL
Another important case is that our web application has pages that should work using SSL. In this case, we automatically select the correct CDN URL for MicrosoftAjax.js. To do this, we just slightly modify the previous code:
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" });
}
}
Problems with localization
When using script localization, which can be the case, for example, if you are using the AJAX Control Toolkit, an unpleasant problem emerged: instead of the correct MicrosoftAjax.js address, ScriptManager tries to load a localized version of the script that is not on the site and the application crashes. This problem can be circumvented by using another parameter to create a ScriptReference, designed to specify the languages ​​for which the localization of this script is supported. If the language of the user who renders the browser is not contained in this list, the default version of the script will be loaded. In the list of languages, you can use any string that, in this case, to our advantage. We modify the example code as follows, specifying “none” as the supported localization language:
sm.Scripts.Add( new ScriptReference {Name = "MicrosoftAjax.js" ,
Path = "http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.js" },
ResourceUICultures = new string []{ "none" } });
* This source code was highlighted with Source Code Highlighter .
Now the correct version of the script will always be loaded.
PS Initially, I translated and posted this article in my personal blog: blog.dvteam.ru . (The original article is here: blog.turlov.com/2009/11/using-microsoft-ajax-library-35-with.html )
Could not resist and duplicated it on Habré. The topic is interesting, and I would not want the information to be lost on my little-known page. This is my first topic, please do not kick much and do not be lazy to write in the comments if you put a minus for what I got it. I am not a telepath and I can not take into account your opinion if you do not express it.
Many thanks to
Pavel_Osipov for help in highlighting the code.