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程序设计有所帮助。
SugarHosts 糖果主机商也算是比较老牌的主机商,从2009年开始推出虚拟主机以来,目前当然还是以虚拟主机为主,也有新增云服务器和独立服务器。早年很多网友也比较争议他们家是不是国人商家,其实这些不是特别重要,我们很多国人商家或者国外商家主要还是看重的是品质和服务。一晃十二年过去,有看到SugarHosts糖果主机商12周年的促销活动。如果我们有需要香港、美国、德国虚拟主机的可以选择,他们家的...
快云科技怎么样?快云科技是一家成立于2020年的新起国内主机商,资质齐全 持有IDC ICP ISP等正规商家。我们秉承着服务于客户服务于大众的理念运营,机器线路优价格低。目前已注册用户达到5000+!主营产品有:香港弹性云服务器,美国vps和日本vps,香港物理机,国内高防物理机以及美国日本高防物理机!产品特色:全配置均20M带宽,架构采用KVM虚拟化技术,全盘SSD硬盘,RAID10阵列, 国...
日本vps云服务器怎么选择?很多人都会遇到日本vps和日本云服务器怎么选择的问题,日本云服务器具有免备案的特点。小编今天就分析一下日本云服务器价格多少钱,以方便大家选购的时候有个更加合适的取舍。日本云服务器租用前比较选择,高性能、安全、高效、免备案日本云服务器是很关键的因素。那么,日本云服务器该怎么选择呢?日本作为我们的邻国,与其贸易、文化往来是比较多的。日本云服务器价格多少钱一年?一、日本·CN...