压缩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程序设计有所帮助。

青云互联-洛杉矶CN2弹性云限时五折,9.5元/月起,三网CN2gia回程,可选Windows,可自定义配置

官方网站:点击访问青云互联官网优惠码:五折优惠码:5LHbEhaS (一次性五折,可月付、季付、半年付、年付)活动方案:的套餐分为大带宽限流和小带宽不限流两种套餐,全部为KVM虚拟架构,而且配置都可以弹性设置1、洛杉矶cera机房三网回程cn2gia 洛杉矶cera机房                ...

美国服务器20G防御 50G防御 688元CN2回国

全球领先的IDC服务商华纳云“美国服务器”正式发售啦~~~~此次上线的美国服务器包含美国云服务器、美国服务器、美国高防服务器以及美国高防云服务器。针对此次美国服务器新品上线,华纳云也推出了史无前例的超低活动力度。美国云服务器低至3折,1核1G5M低至24元/月,20G DDos防御的美国服务器低至688元/月,年付再送2个月,两年送4个月,三年送6个月,且永久续费同价,更多款高性价比配置供您选择。...

水墨云历史黑名单IDC,斟酌选购

水墨云怎么样?本站黑名单idc,有被删除账号风险,建议转出及数据备份!水墨云ink cloud Service是成立于2017年的商家,自2020起开始从事香港、日本、韩国、美国等地区CN2 GIA线路的虚拟服务器租赁,同时还有台湾、国内nat vps相关业务,也有iplc专线产品,相对来说主打的是大带宽服务器产品。注意:本站黑名单IDC,有被删除账号风险,请尽量避免,如果已经购买建议转出及数据备...

js压缩为你推荐
Applicationto我研制千万亿次超级电脑支持ipad支持ipad步骤ioscss3圆角用CSS3怎么实现圆角边框?win7telnet怎样在win7下打开telnet 命令重庆电信宽带管家中国电信电脑管家是什么?怎么样?360chrome360的chrome浏览器进程有点多哦???google分析google analysis干什么用的?
域名网 免费域名注册网站 电信服务器租赁 linuxapache虚拟主机 dreamhost webhostingpad 美国主机论坛 淘宝双十一2018 警告本网站 网通服务器ip qq数据库下载 100m免费空间 宁波服务器 免费全能主机 vip购优惠 hkt 台湾谷歌 in域名 丽萨 lick 更多