3

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>
nekno
  • 19,177
  • 5
  • 42
  • 47
  • Positive you don't have any typos in your actual code in your jquery cdn path? – Jack Apr 28 '15 at 19:59
  • Pretty sure. Triple checked and hard coded the full paths, then tried various CDNs, including jQuery.com, Google, Telerik... – nekno Apr 28 '15 at 21:07
  • I know those CDNs support ssl, but have you tried disabling it? Or maybe put in the https address. – Jack Apr 28 '15 at 21:14
  • I tried `http`, `https`, and `//` (protocol-relative) URLs with `CdnSupportsSecureConnection` enabled and disabled. All the same results. It's counter-intuitive, but the *only* way to actually get HTTPS is to use HTTP URLs and set `CdnSupportsSecureConnection = true`. In reading the `ScriptResourceDefinition` [reference source](http://referencesource.microsoft.com/System.Web.Extensions/UI/ScriptResourceDefinition.cs.html#4334448d9436b240), it's clearly coded to support that use, but still isn't clear why everything else fails. – nekno Apr 28 '15 at 21:32
  • 1
    I used the same code you posted on a blank asp.net website on `.Net 4.5` and everything worked fine with `code.jquery` CDN. I also looked at the reference source for the [AssemblyResourceLoader.GetWebResourceUrlInternal](http://referencesource.microsoft.com/#System.Web/Handlers/AssemblyResourceLoader.cs,5e55f99e13c77246) and everything seems in order. Have you tried with another project? From another computer? – Gabriel GM Apr 29 '15 at 12:39
  • I set up a new project, tested it, and it worked. So, I went back to my original project, tested it, and it worked --- having changed nothing. I've been troubleshooting this in my spare time off and on for days. Ridiculous. – nekno Apr 29 '15 at 18:50
  • any light on this? i am having the same issue – Angel Q Jul 21 '20 at 10:58
  • okay, my problem was that i was using https on the cdn links, because when you copy them they have the https, for some reason with https does not work, but with http it work – Angel Q Jul 22 '20 at 07:38

0 Answers0