简体   繁体   English

ASP.NET脚本组合

[英]ASP.NET Script combining

I have replaced my asp:ScriptManager control with ajaxToolkit:ToolkitScriptManager with attribute CombineScripts="true" . 我用ajaxToolkit:ToolkitScriptManager替换了我的asp:ScriptManager控件,其属性为CombineScripts="true"

Now when I view source of HTML page, there is new script tag: 现在,当我查看HTML页面的源代码时,有一个新的脚本标记:

<script src="/MyPage.aspx?_TSM_HiddenField_=ctl00__pageBody_asScript_tscAjaxScripts_HiddenField&amp;_TSM_CombinedScripts_=%3b%3bAjaxControlToolkit%2c+Version%3d3.0.20820.30277%2c+Culture%3dneutral%2c+PublicKeyToken%3d28f01b0e84b6d53e%3aen-US%3a06e896ab-1f8c-4bcb-9cc4-0200671cba8a%3ae2e86ef9%3a1df13a87%3ac4c00916%3aaf22e781%3a9ea3f0e2%3ac7c04611%3acd120801%3a3858419b%3a96741c43%3a38ec41c0" />

But also are still old script tags: WebResource.axd and several ScriptResource.axd 但也是旧脚本标签: WebResource.axd和几个ScriptResource.axd

How do I remove WebResource.axd and ScriptResource.axd links from the page? 如何从页面中删除WebResource.axdScriptResource.axd链接?

In this scenario by setting CombineScripts="true" of ajaxToolkit:ToolkitScriptManager means this will combine all the script files used by the ajax controls on that page, and it will load the combined script in the below form- 在这种情况下,通过设置CombineScripts="true" ajaxToolkit:ToolkitScriptManager意味着这将组合该页面上的ajax控件使用的所有脚本文件,并将以下面的形式加载组合脚本 -

<script src="/MyPage.aspx?_TSM_HiddenField_=ctl00__pageBody_asScript_tscAjaxScripts_HiddenField&amp;_TSM_CombinedScripts_=%3b%3bAjaxControlToolkit%2c+Version%3d3.0.20820.30277%2c+Culture%3dneutral%2c+PublicKeyToken%3d28f01b0e84b6d53e%3aen-US%3a06e896ab-1f8c-4bcb-9cc4-0200671cba8a%3ae2e86ef9%3a1df13a87%3ac4c00916%3aaf22e781%3a9ea3f0e2%3ac7c04611%3acd120801%3a3858419b%3a96741c43%3a38ec41c0" />

But in ScriptResource.axd files it will load the Microsoft Ajax library files like - MicrosoftAjax.debug.js and MicrosoftAjaxWebForms.debug.js in separate request. 但是在ScriptResource.axd文件中,它将在单独的请求中加载Microsoft Ajax库文件,如MicrosoftAjax.debug.jsMicrosoftAjaxWebForms.debug.js You can make a single request for Microsoft Ajax by combing them. 您可以通过梳理它们来为Microsoft Ajax发出单个请求。

You can use <CompositeScript> tag explicitly and specify MicrosoftAjax.js , Webform.js and MicrosoftAjaxWebForms.debug.js to combine them in a single request. 您可以显式使用<CompositeScript>标记,并指定MicrosoftAjax.jsWebform.jsMicrosoftAjaxWebForms.debug.js以在单个请求中组合它们。 In this way you will see only two request for JS, one for combined script of Ajax controls on that page and another for combined script MicrosoftAjax js 通过这种方式,您将只看到两个JS请求,一个用于该页面上的Ajax控件的组合脚本,另一个用于组合脚本MicrosoftAjax js

There are some components of the AjaxControlToolkit that do not support combining scripts. AjaxControlToolkit中有一些组件不支持组合脚本。 From: http://blogs.msdn.com/b/delay/archive/2007/06/11/script-combining-made-easy-overview-of-the-ajax-control-toolkit-s-toolkitscriptmanager.aspx 来自: http//blogs.msdn.com/b/delay/archive/2007/06/11/script-combining-made-easy-overview-of-the-ajax-control-toolkit-s-toolkitscriptmanager.aspx

The Slider's SliderBehavior.js script uses a fairly obscure feature enabled by the PerformSubstitution property of the WebResource attribute that allows <%= WebResource/ScriptResource %> tags to be embedded in JS files and get resolved before the script is sent to the browser. Slider的SliderBehavior.js脚本使用了一个相当模糊的功能,该功能由WebResource属性的PerformSubstitution属性启用,该属性允许将<%= WebResource / ScriptResource%>标记嵌入到JS文件中,并在脚本发送到浏览器之前得到解析。 This behavior isn't currently supported by ToolkitScriptManager ToolkitScriptManager当前不支持此行为

There's no magical flag you can turn on to completely get rid of these file. 没有神奇的旗帜你可以打开以完全摆脱这些文件。 The advantage of using the ToolKitScriptManager is that it will reduce the number of resources your page requires, leading to better performance. 使用ToolKitScriptManager的优点是它可以减少页面所需的资源数量,从而提高性能。 The Webresource.axd and Scriptresouce.axd files are used by the entire application, and not just the AjaxControlToolkit. Webresource.axd和Scriptresouce.axd文件由整个应用程序使用,而不仅仅是AjaxControlToolkit。 My advice would be to use a program like Fiddler to see what resources the Scriptresource.axd and Webresource.axd are actually pulling in and work from there. 我的建议是使用像Fiddler这样的程序来查看Scriptresource.axd和Webresource.axd实际上从哪里获取和工作的资源。

If you are using .NET 4.0, you could use the enablecdn attribute on the scriptmanager, and it serves the required JavaScript code from the Microsoft's CDN. 如果您使用的是.NET 4.0,则可以使用scriptmanager上的enablecdn属性,它可以从Microsoft的CDN提供所需的JavaScript代码。

If not, you can still merge them as Scott Hanselman describes here . 如果没有,你仍然可以像Scott Hanselman 在这里描述的那样合并它们。 You can also go one step further and use an HTTP Module/Filter to combine those scripts (WebResource.axd, ScriptResource.axd) into 2 or 3 ones, however, it needs an extensive knowledge of ASP.NET foundation; 您还可以更进一步使用HTTP模块/过滤器将这些脚本(WebResource.axd,ScriptResource.axd)组合成2或3个脚本,但是,它需要广泛的ASP.NET基础知识; Or else, you could break the Microsoft Ajax method calls in the app (it's because of those fancy mechanism UpdatePanel uses to update a portion of the page, etc...) 或者,您可以在应用程序中打破Microsoft Ajax方法调用(这是因为UpdatePanel用于更新页面的一部分等等的奇特机制......)

I wrote a simple handler for this that seems to work fine, but it is for ASP.NET MVC, but it shouldn't be that hard to make it work for webforms too. 我为此编写了一个简单的处理程序似乎工作正常,但它适用于ASP.NET MVC,但它也不应该很难使它适用于webforms。 I put it on github: https://github.com/mastoj/SimpleCompression . 我把它放在github上: https//github.com/mastoj/SimpleCompression

The good parts with my solution are: 我的解决方案的好处是:

  • You don't need two copies of your script; 您不需要两个脚本副本; that is, a minified version with the original. 也就是说,原始版本的缩小版本。
  • You can change compress engine if you want. 如果需要,您可以更改压缩引擎。
  • The files are compressed on the fly and put in the cache on the webb server and client (you can update a version setting to force client to download script again). 这些文件即时压缩并放在webb服务器和客户端的缓存中(您可以更新版本设置以强制客户端再次下载脚本)。
  • The files are combined on the fly. 文件即时组合。
  • The combined and compressed file has file dependency to the original files so they are re-compressed and combined if you for some reason need to test something in prod (but don't do it :)). 组合和压缩文件具有与原始文件的文件依赖性,因此如果由于某种原因需要在prod中测试某些东西(但不要这样做:),它们会被重新压缩和组合。
  • And it is easy to use, for MVC at least. 并且它易于使用,至少对于MVC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM