压缩asp.net程序优化js、css实现合并与压缩的方法

js压缩  时间:2021-04-24  阅读:()

asp.net程序优化js、 css实现合并与压缩的方法本文实例讲述了asp.net程序优化js、 css实现合并与压缩的方法。分享给大家供大家参考。具体实现方法如下

访问时将js和css压缩并且缓存在客户端,

采用的是yahoo.yui .compressor组件来完成的,用户可以点击此处本站下载。

创建一个ihttphandler来处理文件

代码如下:publ ic classcombinefi les: ihttphandler

{private const string cachekeyformat=_cachekey_{0}_;private const bool iscompress=true;//需要压缩publ ic bool isreusable

{get

{return false;

}

}publ ic void processrequest(httpcontext context)

{httprequest request=context.request;httpresponse response=context.response;string cachekey=string.empty;string type=request.querystring[type];if(!string. isnul lorempty(type)&& (type==css | | type==js))

{if(type==js)

{response.contenttype=text/javascript;

}else if (type==css)

{response.contenttype=text/css;

}cachekey=string.format(cachekeyformat, type);compresscacheitem cacheitem=httpruntime.cache[cachekey]as compresscacheitem;if(cacheitem==nul l)

{string content=string.empty;string path=context.server.mappath();

//找到这个目录下所有的js或css文件 当然也可以进行配置需求请求压缩哪些文件//这里就将所的有文件都请求压缩string[] fi les=directory.getfi les(path, *.+type);stringbui ldersb=new stringbui lder();

foreach(stringfi lename in fi les)

{if(fi le.exists(fi lename) )

{string readstr=fi le.readal ltext(fi lename,encoding.utf8);sb.append(readstr);

}

}content=sb.tostring();

//开始压缩文件if(iscompress)

{if(type.equals(js))

{content=javascriptcompressor.compress(content);

}else if (type.equals(css))

{content=csscompressor.compress(content);

}

}

//输入到客户端还可以进行gzip压缩,这里就省略了cacheitem = new compresscacheitem() { type = type, content = content, expires =datetime.now.adddays(30) };httpruntime.cache. insert(cachekey, cacheitem, nul l, cacheitem.expires, timespan.zero);}string ifmodifiedsince=request.headers[if-modified-since];if(!string. isnul lorempty(ifmodifiedsince)

&& timespan.fromticks(cacheitem.expires.ticks -datetime.parse(ifmodifiedsince).ticks).seconds 0)

{response.statuscode=(i nt)system.net.httpstatuscode.notmodif ied;response.statusdescription=not modified;

}else

{response.write(cacheitem.content);setcl ientcaching(response, cacheitem.expires);

}

}

}private void setcl ientcaching(httpresponse response,datetime expires)

{response.cache.setetag(datetime.now.ticks.tostring());

response.cache.setlastmodified(datetime.now);

//publ ic以指定响应能由客户端和共享代理缓存进行缓存。response.cache.setcacheabi l ity(httpcacheabi l ity.publ ic);

//是允许文档在被视为陈旧之前存在的最长绝对时间。response.cache.setmaxage(timespan.fromticks(expires.ticks));response.cache.setsl idingexpiration(true);

}private classcompresscacheitem

{

///summary

///类型js或css

////summarypubl ic string type{get; set; }//js css

///summary

///内容

////summarypubl ic string content { set;get; }

///summary

///过期时间

////summarypubl ic datetime expires{ set;get; }

}

}

最后在配置文件中配置一下combinefi les.axd文件具体配置略

引用如下

复制代码代码如下:script type=text/javascript src=/js/combinefi les.axd?type=js/scriptl ink rel=stylesheet type=text/css href=/css/combinefi les.axd?type=css/

希望本文所述对大家的asp.net程序设计有所帮助。

华纳云,3折低至优惠云服务器,独立服务器/高防御服务器低至6折,免备案香港云服务器CN2 GIA三网直连线路月付18元起,10Mbps带宽不限流量

近日华纳云发布了最新的618返场优惠活动,主要针对旗下的免备案香港云服务器、香港独立服务器、香港高防御服务器等产品,月付6折优惠起,高防御服务器可提供20G DDOS防御,采用E5处理器V4CPU性能,10Mbps独享CN2 GIA高速优质带宽,有需要免备案香港服务器、香港云服务器、香港独立服务器、香港高防御服务器、香港物理服务器的朋友可以尝试一下。华纳云好不好?华纳云怎么样?华纳云服务器怎么样?...

热网互联33元/月,香港/日本/洛杉矶/韩国CN2高速线路云主机

热网互联怎么样?热网互联(hotiis)是随客云计算(Suike.Cloud)成立于2009年,增值电信业务经营许可证:B1-20203716)旗下平台。热网互联云主机是CN2高速回国线路,香港/日本/洛杉矶/韩国CN2高速线路云主机,最低33元/月;热网互联国内BGP高防服务器,香港服务器,日本服务器全线活动中,大量七五折来袭!点击进入:热网互联官方网站地址热网互联香港/日本/洛杉矶/韩国cn2...

老周互联24小时无理由退款,香港原生IP,28元起

老周互联怎么样?老周互联隶属于老周网络科技部旗下,创立于2019年12月份,是一家具有代表性的国人商家。目前主营的产品有云服务器,裸金属服务器。创办一年多以来,我们一直坚持以口碑至上,服务宗旨为理念,为用户提供7*24小时的轮班服务,目前已有上千多家中小型站长选择我们!服务宗旨:老周互联提供7*24小时轮流值班客服,用户24小时内咨询问题可提交工单,我们会在30分钟内为您快速解答!另免费部署服务器...

js压缩为你推荐
语句morphvox徐州发布官方微信配置routeCreatedwin7伺服器win7支持ipadeaccelerator使用apmsevr中eAccelerator显示NO是什么问题photoshop技术什么是ps技术win10445端口怎么样打开电脑10800端口itunes备份itunes备份是什么
万网域名注册 合租服务器 网站域名备案 cybermonday westhost 鲨鱼机 parseerror 有奖调查 新家坡 东莞数据中心 网络空间租赁 免费测手机号 网游服务器 支持外链的相册 申请网站 论坛主机 supercache 广州主机托管 国外免费网盘 hdroad 更多