Asp.net程序優(yōu)化js、css如何實現(xiàn)合并與壓縮-創(chuàng)新互聯(lián)

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

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

具體實現(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文件,當然也可以進行配置,需求請求壓縮哪些文件
                    //這里就將所的有文件都請求壓縮
                    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>
            /// 內(nèi)容
            /// </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程序優(yōu)化js、css如何實現(xiàn)合并與壓縮”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當前文章:Asp.net程序優(yōu)化js、css如何實現(xiàn)合并與壓縮-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article6/dppjog.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、小程序開發(fā)定制開發(fā)、面包屑導航、營銷型網(wǎng)站建設、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設網(wǎng)站維護公司