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程序设计有所帮助。
老周互联怎么样?老周互联隶属于老周网络科技部旗下,创立于2019年12月份,是一家具有代表性的国人商家。目前主营的产品有云服务器,裸金属服务器。创办一年多以来,我们一直坚持以口碑至上,服务宗旨为理念,为用户提供7*24小时的轮班服务,目前已有上千多家中小型站长选择我们!服务宗旨:老周互联提供7*24小时轮流值班客服,用户24小时内咨询问题可提交工单,我们会在30分钟内为您快速解答!另免费部署服务器...
昨天有在"盘点2021年主流云服务器商家618年中大促活动"文章中整理到当前年中大促618活动期间的一些国内国外的云服务商的促销活动,相对来说每年年中和年末的活动力度还是蛮大的,唯独就是活动太过于密集,而且商家比较多,导致我们很多新人不懂如何选择,当然对于我们这些老油条还是会选择的,估计没有比我们更聪明的进行薅爆款新人活动。有网友提到,是否可以整理一篇当前的这些活动商家中的促销产品。哪些商家哪款产...
BuyVM测评,BuyVM怎么样?BuyVM好不好?BuyVM,2010年成立的国外老牌稳定商家,Frantech Solutions旗下,主要提供基于KVM的VPS服务器,数据中心有拉斯维加斯、纽约、卢森堡,付费可选强大的DDOS防护(月付3美金),特色是1Gbps不限流量,稳定商家,而且卢森堡不限版权。1G或以上内存可以安装Windows 2012 64bit,无需任何费用,所有型号包括免费的...