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

标准互联(450元)襄阳电信100G防御服务器 10M独立带宽

目前在标准互联这边有两台香港云服务器产品,这不看到有通知到期提醒才关注到。平时我还是很少去登录这个服务商的,这个服务商最近一年的促销信息比较少,这个和他们的运营策略有关系。已经从开始的倾向低价和个人用户云服务器市场,开始转型到中高端个人和企业用户的独立服务器。在这篇文章中,有看到标准互联有推出襄阳电信高防服务器100GB防御。有三款促销方案我们有需要可以看看。我们看看几款方案配置。型号内存硬盘IP...

GreenCloudVPS($30/年),500G大硬盘VPS,10Gbps带宽

GreenCloudVPS最近在新加坡DC2节点上了新机器,Dual Xeon Silver 4216 CPU,DDR4内存,10Gbps网络端口,推出了几款大硬盘VPS套餐,基于KVM架构,500GB磁盘起年付30美元。除了大硬盘套餐外,还加推了几款采用NVMe硬盘的常规套餐,最低年付20美元。不过需要提醒的是,机房非直连中国,尤其是电信用户ping值感人,包括新加坡DC1也是如此。大硬盘VPS...

小渣云(36元/月)美国VPS洛杉矶 8核 8G

小渣云 做那个你想都不敢想的套餐 你现在也许不知道小渣云 不过未来你将被小渣云的产品所吸引小渣云 专注于一个套餐的商家 把性价比 稳定性 以及价格做到极致的商家,也许你不相信36元在别人家1核1G都买不到的价格在小渣云却可以买到 8核8G 高配云服务器,并且在安全性 稳定性 都是极高的标准。小渣云 目前使用的是美国超级稳定的ceranetworks机房 数据安全上 每5天备份一次数据倒异地 支持一...

js压缩为你推荐
伺服器win7支持ipad支持ipadpublicationethics.org支持ipad南京医科大学合同管理系统iphone连不上wifi苹果手机“无法加入网络”怎么办127.0.0.1为什么输入127.0.0.1无法打开页面360chrome360Chrome 世界之窗极速浏览器 ChromePlus迅雷下载速度迅雷下载快慢和什么有关
美国vps主机 免费试用vps 过期域名抢注 cpanel主机 nerd directadmin xfce 远程登陆工具 国内加速器 佛山高防服务器 西安服务器托管 网页加速 中国联通宽带测速 国内空间 博客域名 小夜博客 贵州电信 香港博客 什么是dns 日本小学生 更多