asp.net程序優(yōu)化js、css實現(xiàn)合并與壓縮的方法
來源:易賢網(wǎng) 閱讀:622 次 日期:2014-12-22 15:07:39
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net程序優(yōu)化js、css實現(xiàn)合并與壓縮的方法”,方便廣大網(wǎng)友查閱!

本文實例講述了asp.net程序優(yōu)化js、css實現(xiàn)合并與壓縮的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

訪問時將js和css壓縮并且緩存在客戶端,

采用的是yahoo.yui.compressor組件來完成的,用戶可以點擊此處本站下載。

創(chuàng)建一個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文件,當(dāng)然也可以進行配置,需求請求壓縮哪些文件

//這里就將所的有文件都請求壓縮

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);

}

}

//輸入到客戶端還可以進行g(shù)zip壓縮 ,這里就省略了

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 以指定響應(yīng)能由客戶端和共享(代理)緩存進行緩存。

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>

/// 內(nèi)容

/// </summary>

public string content { set; get; }

/// <summary>

/// 過期時間

/// </summary>

public datetime expires { set; get; }

}

}

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

引用如下

復(fù)制代碼 代碼如下:<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程序設(shè)計有所幫助。

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)