這篇文章主要為大家展示了“Asp.net程序優(yōu)化js、css如何實(shí)現(xiàn)合并與壓縮”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Asp.net程序優(yōu)化js、css如何實(shí)現(xiàn)合并與壓縮”這篇文章吧。

具體實(shí)現(xiàn)方法如下:
訪問(wèn)時(shí)將js和css壓縮并且緩存在客戶端,
采用的是Yahoo.Yui.Compressor組件來(lái)完成的,用戶可以點(diǎn)擊此處本站下載。
創(chuàng)建一個(gè)IHttpHandler來(lái)處理文件
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("");
//找到這個(gè)目錄下所有的js或css文件,當(dāng)然也可以進(jìn)行配置,需求請(qǐng)求壓縮哪些文件
//這里就將所的有文件都請(qǐ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();
// 開(kāi)始?jí)嚎s文件
if (IsCompress)
{
if (type.Equals("js"))
{
content = JavaScriptCompressor.Compress(content);
}
else if (type.Equals("css"))
{
content = CssCompressor.Compress(content);
}
}
//輸入到客戶端還可以進(jìn)行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 以指定響應(yīng)能由客戶端和共享(代理)緩存進(jìn)行緩存。
response.Cache.SetCacheability(HttpCacheability.Public);
//是允許文檔在被視為陳舊之前存在的最長(zhǎng)絕對(duì)時(shí)間。
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>
/// 過(guò)期時(shí)間
/// </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程序優(yōu)化js、css如何實(shí)現(xiàn)合并與壓縮”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:Asp.net程序優(yōu)化js、css如何實(shí)現(xiàn)合并與壓縮-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://chinadenli.net/article6/dppjog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、小程序開(kāi)發(fā)、定制開(kāi)發(fā)、面包屑導(dǎo)航、營(yíng)銷型網(wǎng)站建設(shè)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容