combine scripts in asp.net ajax toolkit

╄→尐↘猪︶ㄣ 提交于 2019-11-30 04:57:51

问题


We are using version 3.0.20229.0 of the asp.net ajaxControlTookKit (before .net 3.5 sp1). I was wondering if I can combine our custom javascript files into the ScriptResource.axd that the controlTookKit creates. I've found this article (http://blogs.msdn.com/delay/archive/2007/06/11/script-combining-made-easy-overview-of-the-ajax-control-toolkit-s-toolkitscriptmanager.aspx) which tells me that I need to add the scriptCombine attribute to the assembly file. We are running a WebSite project, how can I add this attribute?


回答1:


You would need to add the scripts as resources to a seperate library and reference them from there to take advantage of the script combiner.

Edit to provide an walk through

Create a new Class Library project (called for example "CombinedScipts"), remove the default class.

Add a reference to both the AjaxControlToolkit and System.Web

Add your JS files to the project, and change their Build Action property to "Embedded Resource".

Open the AssemblyInfo.cs file

Add the following:

// You need to add a web resource call for each JS file in the project
[assembly: WebResource("CombinedScripts.Console.js", "text/javascript")]
[assembly: WebResource("CombinedScripts.Utilities.js", "text/javascript")]
// Not setting IncludeScripts or ExcludeScripts marks all scripts as
// combinable.
[assembly: AjaxControlToolkit.ScriptCombine()]

Add this library as a reference in your web site project.

In your project, you can then add the following between the ToolkitScriptManager tags:

<Scripts>
  <asp:ScriptReference name="CombinedScripts.Console.js" 
                       assembly="CombinedScripts" />
  <asp:ScriptReference name="CombinedScripts.Utilities.js"
                       assembly="CombinedScripts" />
</Scripts>

Not forgetting to ensure that the CombineScripts property of the ToolkitScriptManager is set to true.

This then results in one call to something like: /pageName.aspx?_TSM_HiddenField_=ToolkitScriptManager1_HiddenField&[...]

Which will have your combined scripts in, with comment delimiters like:

//START CombinedScripts.Console.js
[...]
//END CombinedScripts.Console.js
//START CombinedScripts.Utilities.js
[...]
//END CombinedScripts.Utilities.js


来源:https://stackoverflow.com/questions/626659/combine-scripts-in-asp-net-ajax-toolkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!