asp.net cdn和scriptmanager

时间:2014-09-18 11:15:15

标签: asp.net cdn

我想了解cdn使用。

1 对于jquery:我在使用本地文件的回退情况下阅读了很多帖子

通用解决方案:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/ZZjquery.min.js"></script>

<script type='text/javascript'>
    if (typeof jQuery == 'undefined') {
        document.write(unescape("%3Cscript src='/Scripts/jquery-1.11.1.min.js' type='text/javascript' %3E%3C/script%3E"));
    }
</script>

对于jquery mobile

   <script src="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/ZZjquery.mobile.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.css" />

<script type='text/javascript'>

    if (typeof $.mobile == 'undefined') {
        document.write(unescape("%3Cscript src='/Scripts/jquery.mobile-1.4.4.min.js'  %3E%3C/script%3E"));
        document.write(unescape("%3Clink rel='/stylesheet' href='/Content/JQ/jquery.mobile-1.4.4.css'  %3E%3C/script%3E"));
    }

</script>

我还阅读了一个更好的解决方案:

 void Application_Start(object sender, EventArgs e) {

// map a simple name to a path

ScriptManager.ScriptResourceMapping.AddDefinition("jQuery", new ScriptResourceDefinition {

    Path = "~/scripts/jquery-1.3.2.min.js",

    DebugPath = "~/scripts/jquery-1.3.2.js",

    CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.min.js",

    CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.3.2.js"

});

}

但在这种情况下,如何从本地加载css文件?

2 另一个问题是关于MicrosoftAjax和其他东西呢?

我使用这样的scriptmanger:

<asp:ScriptManager runat="server" EnableCdn="true"  />

我没有在BundleCollection中包含所有WebForms和MicrosoftAjax脚本

但是如果微软Ajax CDN掉线会发生什么?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好的,我用

解决了我的问题

<强> 1

在App_start

 Dim srd As New ScriptResourceDefinition()
            srd.Path = "~/Scripts/jquery-1.11.1.min.js"
            srd.DebugPath = "~/Scripts/jquery-1.11.1.min.js"
            srd.CdnPath = "//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"
            srd.LoadSuccessExpression = "window.jQuery"

            ScriptManager.ScriptResourceMapping.AddDefinition("JQ", srd)

...

然后在我的母版页

<asp:ScriptManager runat="server" EnableCdn="true"  >
        <Scripts>


            <asp:ScriptReference Name="JQ" />
            <asp:ScriptReference Path="~/Scripts/InitMobile.js" />
            <asp:ScriptReference Name="JQM" />
             <asp:ScriptReference Name="JQT" />
            <asp:ScriptReference Name="MyJQ" />
        </Scripts>

    </asp:ScriptManager>

       <asp:ContentPlaceHolder runat="server" ID="ScriptContent" />

我的ScriptContent在我的页面中用于初始化一些jquery的东西

对于css文件,我测试该代码中是否存在其中一个类

<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.css" />
    <script type='text/javascript'>

        try{
            var hasstyle = false;
            var fullstylesheets = document.styleSheets;
            for (var sx = 0; sx < fullstylesheets.length; sx++) {
                var sheetclasses = fullstylesheets[sx].rules || document.styleSheets[sx].cssRules;
                for (var cx = 0; cx < sheetclasses.length; cx++) {
                    if (sheetclasses[cx].selectorText == ".ui-btn") {
                        hasstyle = true; break;
                        //return classes[x].style;              
                    }
                }
            }

        }
        catch (err) { }

            if (!hasstyle) {
                document.write('<link href="<%= Page.ResolveClientUrl("~/Content/JQ/jquery.mobile-1.4.4.css") %>" rel="stylesheet" />');
            }

    </script>

<强> 2

Scriptmanager独自工作......

例如。这个输出

<script src="http://ajax.aspnetcdn.com/ajax/4.5/6/MicrosoftAjaxWebForms.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
(window.Sys && Sys.WebForms)||document.write('<script type="text/javascript" src="../Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"><\/script>');//]]>
</script>