ASP .NET 4.0 in ScriptManager

Novi ASP .NET 4.0 scriptmanager ima precej izboljšav. 


Omogočeno je nalaganje iz CDN iz večja kontrola nad nalaganjem knjižnic.


EnableCdn? Yes please.

This one has been blogged about by ScottGu already, but for completeness, here it is. The ASP.NET Ajax scripts are now hosted in a Microsoft CDN, and you can tell ScriptManager to load them from there by simply enabling this property. For virtually no work you get better performance, less bandwidth usage, and a cleaner rendering due to those ScriptResource.axd urls going away from your HTML. Do read the linked post for details. But wait, there’s more!

What has not really been blogged about is this: The EnableCdn property isn’t only for ASP.NET Ajax scripts. Even the scripts in System.Web.dll are on the CDN. So if you are using a GridView, TreeView, or Validators, for example – those scripts will load from the CDN, too. It wouldn’t have been the best experience if only only some scripts were affected by this setting, right?

But what if you are using custom scripts, or 3rd party controls? How would that work? Here’s how.

Normally, when you embed a script within an assembly for use by a WebForm (either via ScriptManager or the classic GetWebResourceUrl and RegisterScriptResource APIs), you have to define a WebResourceAttribute for it, which allows it to be accessed via the WebResource.axd or ScriptResource.axd handlers:

[assembly: WebResource(“Foo.js”, “application/x-javascript”)]

Now, there’s a new property on WebResourceAttribute: CdnPath. Don’t get too caught up on the fact it’s a hard coded url. More on that later.

[assembly: WebResource(“Foo.js”, “application/x-javascript”, CdnPath = “”)]

ScriptManager looks for this property when the EnableCdn property is set to true, and simply uses that path instead of the ScriptResource.axd path to the assembly resource. Pretty simple. And this means that you to

o can provide your own CDN p






ScriptManager does some interesting and useful things, like: script combining, serving scripts from assemblies, script localization, script globalization, removing of duplicate references, automatic switching between debug and release scripts, and now automatic switching to a CDN. Trouble is, it comes with a bit of a tax: It always includes the Microsoft Ajax library (MicrosoftAjax.js), and by default, MicrosoftAjaxWebForms.js (for partial rendering support with UpdatePanel). The partial rendering script could be removed by setting EnablePartialRendering=false, but there was no way to disable MicrosoftAjax.js. So if you wanted to use the useful features of ScriptManager without using MicrosoftAjax.js, well, you couldn’t (although Bertrand once blogged a way of hacking it by making use of it’s de-duping process).

The AjaxFrameworkMode property adds two new modes to ScriptManager’s behavior. Enabled (the default, and the same behavior as before), Explicit (does not include any scripts by default but still assumes Microsoft Ajax is to be utilized), and Disabled (does not include any scripts by default and does not assume Microsoft Ajax will be used).

So here I have a ScriptManager that is including a custom script, without also loading MicrosoftAjax.js or any of the inline script it normally produces.

<asp:ScriptManager runat=”server” AjaxFrameworkMode=”Disabled”>


        <asp:ScriptReference Path=”~/scripts/foo.js” />


</asp:ScriptManager>aths for your own assembly resource scripts. As for where to host your script, well, that’s up to you.


Celoten članek je tukaj:



Oddajte komentar

Fill in your details below or click an icon to log in: Logo

Komentirate prijavljeni s svojim računom. Odjava /  Spremeni )

Google+ photo

Komentirate prijavljeni s svojim Google+ računom. Odjava /  Spremeni )

Twitter picture

Komentirate prijavljeni s svojim Twitter računom. Odjava /  Spremeni )

Facebook photo

Komentirate prijavljeni s svojim Facebook računom. Odjava /  Spremeni )


Connecting to %s

%d bloggers like this: