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程序设计有所帮助。
BuyVM针对中国客户推出了China Special - STREAM RYZEN VPS主机,带Streaming Optimized IP,帮你解锁多平台流媒体,适用于对于海外流媒体有需求的客户,主机开设在拉斯维加斯机房,AMD Ryzen+NVMe磁盘,支持Linux或者Windows操作系统,IPv4+IPv6,1Gbps不限流量,最低月付5加元起,比美元更低一些,现在汇率1加元=0.7...
RAKsmart怎么样?RAKsmart香港机房新增了付费的DDoS高防保护服务,香港服务器默认接入20Mbps的大陆优化带宽(电信走CN2、联通和移动走BGP)。高防服务器需要在下单页面的IP Addresses Option里面选择购买,分:40Gbps大陆优化高防IP-$461/月、100Gbps国际BGP高防IP-$692/月,有兴趣的可以根据自己的需求来选择!点击进入:RAKsmart官...
a400互联是一家成立于2020年商家,主营美国机房的产品,包括BGP线路、CN2 GIA线路的云服务器、独立服务器、高防服务器,接入线路优质,延迟低,稳定性高,额外也还有香港云服务器业务。当前,全场服务器5折,香港VPS7折,洛杉矶VPS5折,限时促销!A400互联官网:https://a400.net/优惠活动全场独服永久5折优惠(续费同价):0722香港VPS七折优惠:0711洛杉矶VPS五...