简要咨询咨询QQ网站导航网站搜索手机站点联系我们设为首页加入收藏 

asp.net程序优化js、css实现合并与压缩的方法

来源:易贤网   阅读:752 次  日期:2014-12-22 15:07:39

温馨提示:易贤网小编为您整理了“asp.net程序优化js、css实现合并与压缩的方法”,方便广大网友查阅!

本文实例讲述了asp.net程序优化js、css实现合并与压缩的方法。分享给大家供大家参考。具体实现方法如下:

访问时将js和css压缩并且缓存在客户端,

采用的是yahoo.yui.compressor组件来完成的,用户可以点击此处本站下载。

创建一个ihttphandler来处理文件

代码如下:

public class combinefiles : ihttphandler

{

private const string cachekeyformat = _cachekey_{0}_;

private const bool iscompress = true; //需要压缩

public bool isreusable

{

get

{

return false;

}

}

public void processrequest(httpcontext context)

{

httprequest request = context.request;

httpresponse response = context.response;

string cachekey = string.empty;

string type = request.querystring[type];

if (!string.isnullorempty(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 == null)

{

string content = string.empty;

string path = context.server.mappath();

//找到这个目录下所有的js或css文件,当然也可以进行配置,需求请求压缩哪些文件

//这里就将所的有文件都请求压缩

string[] files = directory.getfiles(path, *. + type);

stringbuilder sb = new stringbuilder();

foreach (string filename in files)

{

if (file.exists(filename))

{

string readstr = file.readalltext(filename, 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, null, cacheitem.expires, timespan.zero);

}

string ifmodifiedsince = request.headers[if-modified-since];

if (!string.isnullorempty(ifmodifiedsince)

&& timespan.fromticks(cacheitem.expires.ticks - datetime.parse(ifmodifiedsince).ticks).seconds < 0)

{

response.statuscode = (int)system.net.httpstatuscode.notmodified;

response.statusdescription = not modified;

}

else

{

response.write(cacheitem.content);

setclientcaching(response, cacheitem.expires);

}

}

}

private void setclientcaching(httpresponse response, datetime expires)

{

response.cache.setetag(datetime.now.ticks.tostring());

response.cache.setlastmodified(datetime.now);

//public 以指定响应能由客户端和共享(代理)缓存进行缓存。

response.cache.setcacheability(httpcacheability.public);

//是允许文档在被视为陈旧之前存在的最长绝对时间。

response.cache.setmaxage(timespan.fromticks(expires.ticks));

response.cache.setslidingexpiration(true);

}

private class compresscacheitem

{

/// <summary>

/// 类型 js 或 css

/// </summary>

public string type { get; set; } // js css

/// <summary>

/// 内容

/// </summary>

public string content { set; get; }

/// <summary>

/// 过期时间

/// </summary>

public datetime expires { set; get; }

}

}

最后在配置文件中配置一下combinefiles.axd文件,具体配置略

引用如下

复制代码 代码如下:<script type=text/javascript src=/js/combinefiles.axd?type=js></script>

<link rel=stylesheet type=text/css href=/css/combinefiles.axd?type=css />

希望本文所述对大家的asp.net程序设计有所帮助。

更多信息请查看IT技术专栏

更多信息请查看网络编程
点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】     【点此处查询各地各类考试咨询QQ号码及交流群
上一篇:ado.net执行oracle 存储过程
下一篇:asp.net中利用css实现多界面的两种方法
易贤网手机网站地址:asp.net程序优化js、css实现合并与压缩的方法
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!