我创建了一个新的 ASP.NET 4.5.1 Web 表单项目。
母版页中有一个脚本管理器——它列出了大量的脚本,包括对 jquery 和 bootstrap 的引用:
<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>
这些脚本然后包含在页面中。
我一直认为脚本管理器只是针对 AJAX 相关的脚本,但现在它似乎包括了所有的 js 脚本。它似乎也与 Bundling & Minification 冲突——因为它包含脚本而不是包引用。
我已经搜索过谷歌,但无法找到脚本管理器实际上超出了它与 AJAX 的关系。
更新
我找到了对脚本管理器的 scripts 属性的引用,尽管它没有解释列出所有页面脚本的好处/原因:
http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.scripts(v=vs.110).aspx
最佳答案
这两个功能确实可以协同工作,因为捆绑脚本和管理脚本是两个独立的任务。捆绑显然是两者中更简单的一个,因为它只是指将多个脚本文件合并为一个的技术,以通过减少对服务器的必要请求数量来提高性能。但是,让 ScriptManager 处理捆绑的 JavaScript 文件(以及组成它们的脚本)以及非捆绑的 JavaScript 文件是有充分理由的。例如,您可以使用 ScriptManager 在 Debug模式下加载非缩小脚本和 Release模式下加载缩小脚本之间切换。您还可以指定 LoadSuccessExpression,它将用于检查脚本是否已正确加载,如果未正确加载,则可以根据 CdnPath 属性从 CDN 加载。这些将在 ScriptResourceDefinition 中指定,可能在 App_Code/BundleConfig.cs 或 Global.asax 中,但您仍希望将 ScriptReference 元素添加到 ScriptManager。
关于asp.net - ASP.NET 4.5 Web 窗体中的脚本管理器 - 它有什么作用?你如何使用它的捆绑和缩小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822079/