This is a solution based on ASPnet to automatically merge and output JS and CSS.
First of all, I would like to thank lovecherry's article "another application of httpmodule and httphandler in combination -- merge script style" (http://www.cnblogs.com/lovecherry/archive/2010/04/01/1701997.html). I modified it on the basis of this article, making this module more practical, I made the following changes:
· Simplify tag writing and use <link runat = "server" type = "..." src = "..."/> to represent resources.
· Fixed the issue of JS and CSS generation sequence. The source code's resource merging is asynchronous, and the generation sequence is not certain, which may lead to errors.
· The Group attribute is added to allow direct output of resources in different brains to avoid the problem of inconsistent character sets and oversized files
· Added the merge attribute to control whether a single resource needs to be output in combination.
· Added the compression option to output CSS and JS text after compression.
· Adjust and simplify the project names of some class names
The specific code is not described. You can download it. Instructions for use are as follows:
------------------------------------------------------------
Instructions for use
------------------------------------------------------------
(1) reference a dataset
Resourcemerge. dll
To enable compression, you also need to reference:
Yahoo. Yui. compressor. dll
Ecmascript. net. Modified. dll
(2) modify web. config
<Deleetask>
<Add key = "resourcemergecompress" value = "true"/>
</Appsettings>
<System. Web>
<Httpmodules>
<Add name = "resourcemergemodule" type = "resourcemerge. resourcemergemodule, resourcemerge"/>
</Httpmodules>
<Httphandlers>
<Add Path = "resourcemergehandler. ashx" type = "resourcemerge. resourcemergehandler, resourcemerge" verb = "Get, head"/>
</Httphandlers>
</System. Web>
(3) Use the link label to reference resources
CSS Resources
<Link runat = "server" type = "text/CSS" src = 'content/app.css '/>
The original <link> labels do not need to be changed. You only need to add the runat = "server" attribute.
In fact, if the link label is inside JS Resources
<Link runat = "server" type = "text/JavaScript" href = "content/jquery-1.4.2.min.js"/>
The <link> label is also used. The only difference is that type = "text/JavaScript"
In addition, you can use href or SRC to indicate the resource source.
Additional attributes
GROUP: JS or CSS files in the same group are output in combination to avoid inconsistent character sets or large files.
Merge: true or false, indicating whether to merge and output the resource
------------------------------------------------------------
Example
------------------------------------------------------------
The following labels are used in masterpage, page, and ascx:
<Link runat = "server" type = "text/CSS" group = "Basic" src = "content/grid.css"/>
<Link runat = "server" type = "text/CSS" group = "Basic" src = 'content/define.css '/>
<Link runat = "server" type = "text/CSS" src = 'content/app.css '/>
<Link runat = "server" type = "text/JavaScript" group = "jquery" src = "content/jquery-1.4.2.min.js"/>
<Link runat = "server" type = "text/JavaScript" group = "jquery" src = "content/jquery. Media. js"/>
<Link runat = "server" type = "text/JavaScript" group = "jquery" src = "content/jquery. Metadata. js"/>
<Link runat = "server" type = "text/JavaScript" group = "jquery" src = "content/jquery. bgiframe. js"/>
<Link runat = "server" type = "text/JavaScript" src = "content/json2.js"/>
<Link runat = "server" type = "text/JavaScript" src = "content/swfobject. js'/>
...
The preceding tag may generate the following code to merge and output resources, effectively reducing the number of requests:
<Link href = "/resourcemergehandler. ashx? Type = CSS & amp; Key = a176d36015e217c4503fa63b2176b078 "rel =" stylesheet "type =" text/CSS "/>
<Link href = "/resourcemergehandler. ashx? Type = CSS & amp; Key = 925f680a7156ef593595248981518adb "rel =" stylesheet "type =" text/CSS "/>
<SCRIPT src = "/resourcemergehandler. ashx? Type = JS & amp; Key = cc2081a83fc0cd8f0b6d6e26f81019fa "type =" text/JavaScript "> </SCRIPT>
<SCRIPT src = "/resourcemergehandler. ashx? Type = JS & amp; Key = a6dfeb50fb9be04e83ab2d17a1709f16 "type =" text/JavaScript "> </SCRIPT>
------------------------------------------------------------
Remarks
------------------------------------------------------------
Description
· JS or CSS in the same group are merged and output. The preceding Code JS is divided into two groups (jquery and default) and CSS is divided into two groups (basic and default)
· You are responsible for character encoding in the same group. If the characters are inconsistent, the generated JS Code may fail.
· The generated order is the same as the tag order.
· Resources that can be compressed and output merged
Tag Selection
In the previous version, the <SRC> label is used to describe resources. It is found that the changes are too large in actual use.
Therefore, we can change our thinking to see if we can automatically implement the function of merging and outputting resources with minimal changes.
(1) CSS is very easy. You can directly use the <link> label without any changes. You can use htmlgenericcontrol to obtain the module code.
(2) JS is a little troublesome.
If the runat = "server" attribute is added, ASPnet considers this as a server script, and the page output ends directly.
If the runat attribute is not added, ASPnet parses the label and other texts into literalcontrol, which is difficult to split.
Therefore, the original script tag cannot be retained. The simplest consideration is to change it to the link tag, which requires less code.
Another problem is that if the relative image path is used in the CSS file and CSS is merged, the image cannot be found. The following solutions are available:
(1) Change the absolute image path to the CSS file.
(2) Use the merge = "false" attribute to skip the CSS resource and do not merge the resource.