asp.net - 如何使用 ToolkitScriptManager 复合脚本包含大量 Javascript 文件?

标签 asp.net ajaxcontroltoolkit

如果脚本管理器中的复合脚本包含太多 javascript 文件,您会收到此错误...

“资源 URL 不能超过 1024 个字符。如果使用 CompositeScriptReference,请减少它包含的 ScriptReference 数量,或者将它们合并到单个静态文件中,并将 Path 属性设置为它的位置。”

我知道如何使用旧的 asp:scriptmanager(使用脚本管理器代理)来解决此问题。但如何使用 ToolkitScriptManager 修复它?

<ajaxToolkit:ToolkitScriptManager ID="GeneralScriptManager" CombineScripts="true" CombineScriptsHandlerUrl="Utility/CombineScriptsHandler.ashx" runat="server" AsyncPostBackTimeout="480" EnablePageMethods="true">
    <CompositeScript>
        <Scripts>
            <asp:ScriptReference Path="JavaScript/jQuery/jquery-1.4.1.min.js" />
            ... A whole lot more script references here
        </Scripts>
    </CompositeScript>
</ajaxToolkit:ToolkitScriptManager>

更新 我尝试使用示例应用程序中包含的脚本组合器来执行此操作。我认为这是对的,但我之前已经错了很多次了……不过运气不好。

另一个更新 因此,我认为如果我在单个 ToolkitScriptManager 中包含多个 CompositeScript block ,它可能会将它们全部创建为单独的文件。没有。仍然得到同样的错误。没有人有任何想法吗?当我 Google“toolkitscriptmanager 不能超过 1024 个字符”时,这个 StackOverflow 问题是第 1 个结果...

最佳答案

关于这方面的 MSDN 文档实际上非常好:

The number of script references that a CompositeScriptReference instance can contain is limited by the size of the resulting URL. The URL cannot be longer than 1024 characters.

If you have to work around this limitation, you have two options. The first option is to reduce the number of ScriptReference objects that the composite script contains. The second option is to manually combine the scripts into a single static file. In that case, you can set the Path property to the location of the static file.

如果您想要合并多个物理文件,那么您的做法是错误的 - 请参阅下面的链接 [1]:

You can combine multiple physical script files using that feature, but we really don't recommend it as there is some server overhead related to file monitoring. What you're describing is much better handled by "building" your scripts at compile time instead of doing the combination at runtime. The feature really is for application developers who want to combine existing scripts from various components that they use in their application.

我想要的是一个结合静态 *.js 文件的构建后任务,例如 [2]。

祝你好运。

[1] - http://weblogs.asp.net/bleroy/archive/2008/06/12/script-reference-profiler.aspx

[2] - http://encosia.com/2009/05/20/automatically-minify-and-combine-javascript-in-visual-studio/

关于asp.net - 如何使用 ToolkitScriptManager 复合脚本包含大量 Javascript 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3376063/

相关文章:

ASP.NET - 单击按钮时将参数传递给服务器

javascript - asp.net自动生成Javascript吗?

javascript - Asp.NET 内联脚本中的空格

asp.net - Web 服务器上的 system.web.routing 不工作

c# - 如何使用 Visual Studio Code 引用 System.DirectoryServices 程序集?

.net - 事件不是从嵌套的 Accordion 控件中触发的

ASP.Net Modal Popup 位置

asp.net - 在 asp.net ajax 工具包中组合脚本

jquery - Microsoft Ajax 控制工具包与 jQuery

javascript - 将文本插入所见即所得编辑器问题