ScriptManager仅允许使用Microsoft ASP.NET CDN进行jQuery CDN后备

时间:2015-04-28 18:55:31

标签: c# jquery asp.net ajax microsoft-cdn

如何为我的自定义jQuery ScriptManager使用ScriptResourceDefinition CDN后备的任何CDN?

当我使用ajax.aspnetcdn.com时,我的浏览器会正确地使用CDN网址呈现<script>标记,然后编写回退代码以使用本地网址编写新的<script>标记。

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"
    });

输出

<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>

问题

但是,如果我使用code.jquery.comajax.googleapis.com作为CDN服务器,无论如何我都会获得本地副本:

CS

...<snip>...
CdnDebugPath = "http://code.jquery.com/jquery-" + jQueryVersion + ".js",
CdnPath = "http://code.jquery.com/jquery-" + jQueryVersion + ".min.js",
...<snip>...

输出

<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>

0 个答案:

没有答案