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程序设计有所帮助。
vpsdime怎么样?vpsdime是2013年成立的国外VPS主机商,以大内存闻名业界,主营基于OpenVZ和KVM虚拟化的Linux套餐,大内存、10Gbps大带宽、大硬盘,有美国西雅图、达拉斯、新泽西、英国、荷兰机房可选。在上个月搞了一款达拉斯Linux系统VPS促销,详情查看:vpsdime夏日促销活动,美国达拉斯vps,2G内存/2核/20gSSD/1T流量,$20/年,此次推出一款Wi...
野草云服务器怎么样?野草云是一家成立了9年的国人主机商家,隶属于香港 LucidaCloud Limited (HongKong Registration No. 2736053 / 香港網上查冊中心)。目前,野草云主要销售香港、美国的VPS、虚拟主机及独立服务器等产品,本站也给大家分享过多次他家的优惠了,目前商家开启了优惠活动,香港/美国洛杉矶CN2+BGP云服务器,1核1G仅38元/月起!点击...
LOCVPS发来了针对XEN架构VPS的促销方案,其中美国洛杉矶机房7折,其余日本/新加坡/中国香港等机房全部8折,优惠后日本/新加坡机房XEN VPS月付仅29.6元起。这是成立较久的一家国人VPS服务商,目前提供美国洛杉矶(MC/C3)、和中国香港(邦联、沙田电信、大埔)、日本(东京、大阪)、新加坡、德国和荷兰等机房VPS主机,基于XEN或者KVM虚拟架构,均选择国内访问线路不错的机房,适合建...