ASP.NET下使用Combres对JS、CSS合并和压缩

记录一下,如何简单快捷压缩js和css,通过合并来减少请求次数。

用到的网址:

http://www.nuget.org/packages/combres/

https://github.com/buunguyen/combres

第一步(通过NuGet添加combres):

PM> Install-Package combres

完成之后在web下会生成一个combres.readme,请先阅读。

 ASP.NET MVC:PM> Install-Package Combres.Mvc

 ASP.NET WebForm: PM> Install-Package Combres

第二步:

编辑App_Data/combres.xml文件, 添加所要用的JS 和 CSS ,如:

<?xml version="1.0" encoding="utf-8" ?>
<!--
  This file contains basic settings needed for most web apps.
  For full Combres settings (with explanation), refer to the sample definition file: combres_full_with_annotation.xml
  Also, refer to Combres' documentation: http://combres.codeplex.com/documentation    
-->
<combres xmlns='urn:combres'>
  <filters>
    <filter type="Combres.Filters.FixUrlsInCssFilter, Combres" />
  </filters>
  <!--
    defaultDuration 默认缓存的时间,单位为天数。
    defaultVersion 合并后的资源版本,在你修改了资源文件后需要对版本进行修改,你可以指定auto或者手动设置一个版本号。
    defaultDebugEnabled true:对资源文件不压缩;false:对资源文件压缩。
  -->
  <resourceSets url="~/combres.axd"
                defaultDuration="30"
                defaultVersion="auto"
                defaultDebugEnabled="false" 
                defaultIgnorePipelineWhenDebug="true"
                localChangeMonitorInterval="30"
                remoteChangeMonitorInterval="60"
                >
    <resourceSet name="siteCss" type="css">
      <resource path="~/Style/css/style.css" />
      <resource path="~/Plugin/layer/skin/layer.ext.css" />
    </resourceSet>
    <resourceSet name="siteJs" type="js">
      <resource path="~/Script/jquery-1.7.2.js" />
      <resource path="~/Plugin/layer/lang-cn.js" />
      <resource path="~/Plugin/layer/layer-sc.js" />
      <resource path="~/Script/layeragent.js" />
      <resource path="~/Script/core.js" />
      <resource path="~/Style/js/common.js" />
    </resourceSet>
  </resourceSets>
</combres>

第三步(页面使用):

WebForm:

   <%= WebExtensions.CombresLink("siteCss") %>  
   <%= WebExtensions.CombresLink("siteJs") %>

如:

MVC:

   Rezor:
   @using Combres.Mvc 
   @Url.CombresLink("siteCss")
   @Url.CombresLink("siteJs")

第四步:

运行项目就可以了。

原文地址:https://www.cnblogs.com/wz122889488/p/6379114.html