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

百纵科技:美国独立服务器租用/高配置;E52670/32G内存/512G SSD/4IP/50M带宽,999元/月

百纵科技怎么样?百纵科技国人商家,ISP ICP 电信增值许可证的正规公司,近期上线美国C3机房洛杉矶独立服务器,大带宽/高配置多ip站群服务器。百纵科技拥有专业技术售后团队,机器支持自动化,自助安装系统 重启,开机交付时间 30分钟内交付!美国洛杉矶高防服务器配置特点: 硬件配置高 线路稳定 洛杉矶C3机房等级T4 平价销售,支持免费测试,美国独服适合做站,满意付款。点击进入:百纵科技官方网站地...

野草云提供适合入门建站香港云服务器 年付138元起 3M带宽 2GB内存

野草云服务商在前面的文章中也有多次提到,算是一个国内的小众服务商。促销活动也不是很多,比较专注个人云服务用户业务,之前和站长聊到不少网友选择他们家是用来做网站的。这不看到商家有提供香港云服务器的优惠促销,可选CN2、BGP线路、支持Linux与windows系统,支持故障自动迁移,使用NVMe优化的Ceph集群存储,比较适合建站用户选择使用,最低年付138元 。野草云(原野草主机),公司成立于20...

提速啦(24元/月)河南BGP云服务器活动 买一年送一年4核 4G 5M

提速啦的来历提速啦是 网站 本着“良心 便宜 稳定”的初衷 为小白用户避免被坑 由赣州王成璟网络科技有限公司旗下赣州提速啦网络科技有限公司运营 投资1000万人民币 在美国Cera 香港CTG 香港Cera 国内 杭州 宿迁 浙江 赣州 南昌 大连 辽宁 扬州 等地区建立数据中心 正规持有IDC ISP CDN 云牌照 公司。公司购买产品支持3天内退款 超过3天步退款政策。提速啦的市场定位提速啦主...

js压缩为你推荐
支持ipadboxiphoneiphone连不上wifi苹果手机“无法加入网络”怎么办127.0.0.1传奇服务器非法网关连接: 127.0.0.1canvas2动漫cv井口裕香,都有哪些作品?联通版iphone4s怎么知道到苹果4s是联通版,还是移动版google图片搜索谁能教我怎么在手机用google的图片搜索啊!!!ipad上不了网ipad连上家里的无线却不能上网morphvoxpro怎么用Morphvox pro 变声器 怎么用? 怎么在录音的时候有歌曲的曲子?ios10.0.3ios10.0.2与i0S10.3.3区别
青岛虚拟主机 看国外视频直播vps 域名备案流程 工信部域名备案系统 私服服务器 轻博客 777te 全站静态化 中国电信测速112 200g硬盘 双十一秒杀 万网空间购买 申请免费空间和域名 双线机房 美国独立日 空间购买 免费asp空间 东莞服务器托管 防cc攻击 国外代理服务器 更多