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

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

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

盘点618年中大促中这款云服务器/VPS主机相对值得选择

昨天有在"盘点2021年主流云服务器商家618年中大促活动"文章中整理到当前年中大促618活动期间的一些国内国外的云服务商的促销活动,相对来说每年年中和年末的活动力度还是蛮大的,唯独就是活动太过于密集,而且商家比较多,导致我们很多新人不懂如何选择,当然对于我们这些老油条还是会选择的,估计没有比我们更聪明的进行薅爆款新人活动。有网友提到,是否可以整理一篇当前的这些活动商家中的促销产品。哪些商家哪款产...

Buyvm:VPS/块存储补货1Gbps不限流量/$2起/月

BuyVM测评,BuyVM怎么样?BuyVM好不好?BuyVM,2010年成立的国外老牌稳定商家,Frantech Solutions旗下,主要提供基于KVM的VPS服务器,数据中心有拉斯维加斯、纽约、卢森堡,付费可选强大的DDOS防护(月付3美金),特色是1Gbps不限流量,稳定商家,而且卢森堡不限版权。1G或以上内存可以安装Windows 2012 64bit,无需任何费用,所有型号包括免费的...

js压缩为你推荐
请务必阅读正文之后的免责条款部分经济开发区127支持ipad孩子appleitunes备份itunes备份是什么kb4012598win7怎么查看电脑是否安装了 ms17 010迅雷雷鸟啊啊,想下载《看门狗》可13GB的大小,我每秒才450KB,我该怎么样才能大幅度地免费提高电脑下载altools.u32Authorware 里怎样才能调用flash播放器、播放课件ios11.0.2ios11.0.2怎么降级ios10.3.3微信5.0是哪一年的安卓什么时候能更新微信5.0版本?
域名抢注 cc域名 过期备案域名查询 便宜vps 香港vps主机 火山主机 idc评测 百度云100as debian6 国外在线代理 工作站服务器 qq云端 购买国外空间 smtp服务器地址 全能空间 广州虚拟主机 贵阳电信测速 lamp的音标 登陆qq空间 空间服务器 更多