Asp.net程序優(yōu)化js、css實(shí)現(xiàn)合并與壓縮的方法
本文實(shí)例講述了Asp.net程序優(yōu)化js、css實(shí)現(xiàn)合并與壓縮的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
訪(fǎng)問(wèn)時(shí)將js和css壓縮并且緩存在客戶(hù)端,
采用的是Yahoo.Yui.Compressor組件來(lái)完成的,用戶(hù)可以點(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);
}
}
//輸入到客戶(hù)端還可以進(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)能由客戶(hù)端和共享(代理)緩存進(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>
/// 類(lèi)型 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文件,具體配置略
引用如下
<link rel="stylesheet" type="text/css" href="/css/CombineFiles.axd?type=css" />
希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。
相關(guān)文章
.net SMTP發(fā)送Email實(shí)例(可帶附件)
本文為大家詳細(xì)介紹下.net SMTP發(fā)送Email同時(shí)可帶附件的具體實(shí)現(xiàn)思路及代碼,想實(shí)現(xiàn)的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07Sqlite 常用函數(shù)封裝提高Codeeer的效率
以下是頻繁用到的Sqlite函數(shù),內(nèi)容格式相對(duì)固定,封裝一下有助于提高開(kāi)發(fā)效率^_^至少提高Codeeer的效率了2012-12-12.net泛型通用函數(shù)的特殊問(wèn)題的解決方法
本文介紹了.net泛型通用函數(shù)的特殊問(wèn)題的解決方法,需要的朋友可以參考一下2013-02-02Ext.net中的MessageBox的簡(jiǎn)單應(yīng)用實(shí)現(xiàn)代碼
今天在項(xiàng)目中要用到簡(jiǎn)單的MessageBox的使用。在Ext.net中沒(méi)有Winform那么簡(jiǎn)單,直接寫(xiě)MessageBox.show()的方法。于是就去examples.ext.net的例子網(wǎng)站找,想找到個(gè)實(shí)際能用的2012-03-03淺談.NET中加密和解密的實(shí)現(xiàn)方法分享
這篇文章介紹了.NET中加密和解密的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-11-11CKEditor與dotnetcore實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了CKEditor與dotnetcore實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09.net core實(shí)用技巧——將EF Core生成的SQL語(yǔ)句顯示在控制臺(tái)中
這篇文章主要介紹了如何將EF Core生成的SQL語(yǔ)句顯示在控制臺(tái)中,幫助大家更好的理解和學(xué)習(xí).net core,感興趣的朋友可以了解下2020-08-08