简体   繁体   English

组合Web服务器控件Web和脚本资源(AXD文件)

[英]Combine web server controls web and script resources (axd files)

I know there are lots of similar questions like this in the web, but I haven't got to one working solution after searching for these last 2 days. 我知道网络上有很多类似的问题,但是在搜索这两天后,我还没有找到一个可行的解决方案。 Some threads are outdated, others solutions don't work, others are too complicated to manage, so... Yes, another question about resources.axd files in asp.net projects. 有些线程已经过时,有些解决方案无法正常工作,有些则过于复杂而难以管理,因此...是的,关于asp.net项目中的resources.axd文件的另一个问题。

I'm starting to build a set of server controls and embedding some resources. 我开始构建一组服务器控件并嵌入一些资源。 After building my controls and dragging them into pages, each individual resource is requested by the browser in the form of an .axd script. 构建控件并将其拖到页面中后,浏览器会以.axd脚本的形式请求每个单独的资源。 I understand the part where the ScriptManager manages these scripts and they're not ready to use, for instance, in Bundles in some sort of .Include("*.axd"). 我了解ScriptManager负责管理这些脚本的部分,并且这些脚本还没有准备好使用,例如,在捆绑包中以某种.include(“ *。axd”)形式使用。

  • I tried some of the combine/minify/compress/gzip/icecreamOnTop packages out there but couldn't manage none of them to work. 我尝试了其中的某些Combine / minify / compress / gzip / icecreamOnTop软件包,但无法管理它们。

  • I tried the StriptManager CompositeScript approach and the ScriptResources.axd are indeed combined, the response is successful (code 200), but in the end the scripts don't work. 我试过将StriptManager CompositeScript方法和ScriptResources.axd确实结合在一起,响应成功(代码200),但最终脚本不起作用。 Example: I included the jQuery lib in that composite script and then tried to use it in the page - didn't work. 示例:我将jQuery lib包含在该复合脚本中,然后尝试在页面中使用它-无效。 I must say I didn't reference () the composite script anywhere because I didn't understand how to do it. 我必须说我没有在任何地方引用()复合脚本,因为我不知道如何去做。 If I set the path, then a 404 was returned (found the 1024 byte limit threads, etc...) and all the requests to the WebResources.axd would still remain. 如果我设置路径,则返回404(找到1024个字节的限制线程,等等),并且对WebResources.axd的所有请求仍将保留。

  • I would prefer not to write an HttpHandler myself. 我不想自己写一个HttpHandler。

  • Also tried to download the AjaxToolkit and tried the ToolkitScriptManager which combines scripts, but that added ~4seconds to my page load. 还尝试下载AjaxToolkit,并尝试使用结合了脚本的ToolkitScriptManager,但是这增加了约4秒的页面加载时间。 No, thanks. 不用了,谢谢。

My question is: what is the current approach regarding this matter in .net 4.5? 我的问题是:.net 4.5中关于此问题的当前方法是什么?

I will have lots of resources and would like to combine them. 我将有很多资源,并希望将它们结合起来。 All js and css files in the final website project are bundled, but how to 'bundle' the axd files? 最终网站项目中的所有js和css文件都捆绑在一起,但是如何“捆绑” axd文件?

Here's a little example which will make the following requests: 这是一个小示例 ,它将发出以下请求:

Click here to view image 点击这里查看图片

I know I can set the AjaxFrameworkMode to Disabled and will only have the 2 first WebResources.axd requests in this example, but what about when I have 10 css files embedded in my controls? 我知道我可以将AjaxFrameworkMode设置为Disabled,并且在此示例中仅具有2个第一个WebResources.axd请求,但是当我在控件中嵌入10个CSS文件时该怎么办?

Any working solution would be much appreciated. 任何有效的解决方案将不胜感激。

Many thanks 非常感谢

I managed to implement something that suits my case. 我设法实现了适合自己情况的方法。 More of a work around than a proper combining solution, but works and that's good enough for now. 除了适当的组合解决方案以外,还可以做更多的工作,但是可以正常工作了。

  • All my controls extend a base class which has a property called ProcessResources. 我所有的控件都扩展了一个基类,该基类具有名为ProcessResources的属性。 By default this property is set to true. 默认情况下,此属性设置为true。 That means that JS and CSS resources will be registered and each control has everything to work as standalone (axd resources will be generated). 这意味着将注册JS和CSS资源,并且每个控件都具有独立运行的所有功能(将生成axd资源)。 When set to false, no resource is registered and no axd files are generated. 设置为false时,不注册任何资源,也不生成axd文件。

  • The base class also has a static method that receives a destination path as a parameter and copies all the JS and CSS resources to that folder (if they don't exist or have been modified). 基类还具有一个静态方法,该方法接收目标路径作为参数,并将所有JS和CSS资源复制到该文件夹​​(如果它们不存在或已被修改)。 This method returns a collection of all the needed resources for the control to work. 此方法返回控件正常工作所需的所有资源的集合。

This way, on Application_Start, I can do something like: 这样,在Application_Start上,我可以执行以下操作:

myJsBundle.Inlude(ServerControlA.GetResources("~/ExternalResources", RESOURCES.JAVASCRIPT)); myJsBundle.Inlude(ServerControlA.GetResources(“〜/ ExternalResources”,RESOURCES.JAVASCRIPT));

Then just need to set the ProcessResources flag to false (each control or web.config). 然后只需将ProcessResources标志设置为false(每个控件或web.config)。 All resources are now bundled and no axd files are generated. 现在所有资源都捆绑在一起,并且不生成axd文件。

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

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