最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Asp.net程序優(yōu)化js、css如何實(shí)現(xiàn)合并與壓縮-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“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)合并與壓縮”這篇文章吧。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到龍沙網(wǎng)站設(shè)計(jì)與龍沙網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋龍沙地區(qū)。

具體實(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
        {
            /// 
            /// 類型 js 或 css
            /// 
            public string Type { get; set; } // js css 
            /// 
            /// 內(nèi)容
            /// 
            public string Content { set; get; }
            /// 
            /// 過(guò)期時(shí)間
            /// 
            public DateTime Expires { set; get; }
        }
}

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

引用如下


以上是“Asp.net程序優(yōu)化js、css如何實(shí)現(xiàn)合并與壓縮”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


名稱欄目:Asp.net程序優(yōu)化js、css如何實(shí)現(xiàn)合并與壓縮-創(chuàng)新互聯(lián)
鏈接分享:http://fisionsoft.com.cn/article/dppjog.html