javascript 如何在 asp.net 4.5 中使用 <asp:scriptreference> 包含最新的 Jquery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22349978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:58:48  来源:igfitidea点击:

How to include latest Jquery using <asp:scriptreference> in asp.net 4.5?

c#javascriptasp.netscriptmanager

提问by juhi

i want to include latest jquery in my web application. Bydefault jquery 1.7.1 is getting load.

我想在我的 Web 应用程序中包含最新的 jquery。默认情况下 jquery 1.7.1 正在加载。

i know following code is responsible for that. but what should i do to load jquery 1.10?

我知道以下代码对此负责。但是我应该怎么做才能加载 jquery 1.10?

  <asp:ScriptManager runat="server">
        <Scripts>
            <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
            <%--Framework Scripts--%>
            <asp:ScriptReference Name="MsAjaxBundle" />
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="bootstrap" />
            <asp:ScriptReference Name="respond" />
            <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
            <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
            <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
            <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
            <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
            <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
            <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
            <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
            <asp:ScriptReference Name="WebFormsBundle" />
            <%--Site Scripts--%>
        </Scripts>
    </asp:ScriptManager>

回答by Onur Topal

To do that you have to create your own JS bundle it is usually in Global.asax or App_Start/BundleConfig.cs

为此,您必须创建自己的 JS 包,它通常在 Global.asax 或 App_Start/BundleConfig.cs 中

in there you have to add something like below this keep changes according to version so might be little different.

在那里,您必须添加如下内容,根据版本保持更改,因此可能略有不同。

ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
    {
        Path = "~/Scripts/jquery-" + str + ".min.js", //your path will be ignored
        DebugPath = "~/Scripts/jquery-" + str + ".js",  //your path will be ignored 
        **CdnPath = "http://code.jquery.com/jquery.min.js",** 
        **CdnDebugPath = "http://code.jquery.com/jquery-latest.js"**, 
        CdnSupportsSecureConnection = true, 
        LoadSuccessExpression = "window.jQuery"
    }); 

and finally enable cdn in for the script manager

最后为脚本管理器启用 cdn

<asp:ScriptManager EnableCdn="True" />

might look little complicate but once you set it will work just fine.

可能看起来有点复杂,但一旦你设置它就会工作得很好。