How can I use any CDN with ScriptManager
CDN fallback for my custom jQuery ScriptResourceDefinition
?
When I use ajax.aspnetcdn.com
, my browser properly renders the <script>
tag with the CDN URL, then writes the fallback code to write a new <script>
tag with the local URL.
ASPX
<asp:ScriptManager ID="scriptManager" runat="server" EnablePartialRendering="true" EnableCdn="true" EnableCdnFallback="true">
<Scripts>
<asp:ScriptReference Name="jquery" />
</Scripts>
</asp:ScriptManager>
CS
string jQueryVersion = "1.11.2";
ScriptManager.ScriptResourceMapping.AddDefinition(
"jquery",
new ScriptResourceDefinition()
{
DebugPath = "~/Scripts/jquery-" + jQueryVersion + ".js",
Path = "~/Scripts/jquery-" + jQueryVersion + ".min.js",
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + jQueryVersion + ".js",
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + jQueryVersion + ".min.js",
CdnSupportsSecureConnection = true,
LoadSuccessExpression = "window.jQuery"
});
Output
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
(window.jQuery)||document.write('<script type="text/javascript" src="Scripts/jquery-1.11.2.min.js"><\/script>');//]]>
</script>
Problem
If I use code.jquery.com
or ajax.googleapis.com
as the CDN server, however, I just get the local copy no matter what:
CS
...<snip>...
CdnDebugPath = "http://code.jquery.com/jquery-" + jQueryVersion + ".js",
CdnPath = "http://code.jquery.com/jquery-" + jQueryVersion + ".min.js",
...<snip>...
Output
<script src="Scripts/jquery-1.11.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
(window.jQuery)||document.write('<script type="text/javascript" src="Scripts/jquery-1.11.2.min.js"><\/script>');//]]>
</script>