天天看點

談mvc開發中gzip壓縮的應用

壓縮view的内容,可加過濾器

    public class GzipFilter : ActionFilterAttribute

    {

        public override void OnResultExecuting(ResultExecutingContext filterContext)

        {

            string acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];

            if (String.IsNullOrEmpty(acceptEncoding)) return;

            var response = filterContext.HttpContext.Response;

            acceptEncoding = acceptEncoding.ToUpperInvariant();

            if (acceptEncoding.Contains("GZIP"))

            {

                response.AppendHeader("Content-Encoding", "gzip");

                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);

            }

            else if (acceptEncoding.Contains("DEFLATE"))

                response.AppendHeader("Content-Encoding", "deflate");

                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);

        }

    }

然後在要壓縮的頁面控制器上加标簽。

        [GzipFilter]

        public ActionResult Index()

現在基本上所有的浏覽器支援gzip, deflate.

這裡是程式設計對css和js檔案進行壓縮放在本地,然後發送給用戶端。

----這種方法在iis7.5的內建模式下有效,在vs中有效,但在iis6裡我還沒配置好,無效

----關鍵是請求,隻對action有效,像js,css檔案的請求,在BeginRequest裡檢測不到。這種方法運作在iis7裡很完美,檔案大概會被壓縮到原來的1/3到1/4.

此方法主要是給請求的檔案加上http頭//Response.AppendHeader("Content-Encoding", "gzip"); 這裡很難處理。

如果有誰找到iis6裡面可以運作的方法麻煩告訴我,或許能一起讨論找到更好的解決方案,非常感謝!

[email protected]

浏覽器檢測到這個頭,就會對檔案進行解壓縮,就正常運作了。

        protected void Application_BeginRequest(object sender, EventArgs e)

            GzipFiles();

        private void GzipFiles()

            string acceptEncoding = Request.Headers["Accept-Encoding"];

            string filepath = Request.FilePath;

            string mapfilepath = Server.MapPath("~" + filepath);

            if (acceptEncoding.Contains("gzip"))

                #region Gzip處理

                if (filepath.EndsWith(".css"))//css檔案處理

                {

                    Response.AppendHeader("Content-Type", "text/css");

                    Request.ContentType = "text/css";

                    if (filepath.EndsWith("gzip.css"))

                    {

                        FileInfo fi = new FileInfo(mapfilepath);

                        Response.AppendHeader("Content-Encoding", "gzip");

                        int len = mapfilepath.Length - "gzip.css".Length;

                        if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);

                    }

                }

                else if (filepath.EndsWith(".js"))//js檔案處理

                    Response.AppendHeader("Content-Type", "application/x-javascript");

                    Request.ContentType = "application/x-javascript";

                    if (filepath.EndsWith("gzip.js"))

                        int len = mapfilepath.Length - "gzip.js".Length;

                #endregion

            else if (acceptEncoding.Contains("deflate"))

                #region deflate處理

                    if (filepath.EndsWith("deflate.css"))

                        int len = mapfilepath.Length - "deflate.css".Length;

                    if (filepath.EndsWith("deflate.js"))

                        int len = mapfilepath.Length - "deflate.js".Length;

        public void GZip(string fileName, string gipFileName)

            FileStream fr = File.Create(gipFileName);

            FileStream fc = File.OpenRead(fileName);

            GZipStream gzs = new GZipStream(fr, CompressionMode.Compress); //壓縮檔案類

            byte[] arr = new byte[fc.Length];

            fc.Read(arr, 0, (int)fc.Length);

            gzs.Write(arr, 0, (int)fc.Length);

            gzs.Close();

            fc.Close();

            fr.Close();

        //解壓縮檔案方法

        public void DeZGip(string fileName, string gipFileName)

            //準備輸入輸出檔案

            FileStream fc = File.Create(fileName);

            FileStream fr = File.OpenRead(gipFileName);

            GZipStream gzs = new GZipStream(fr, CompressionMode.Decompress);

            byte[] arr = new byte[fr.Length];

            fr.Read(arr, 0, (int)fr.Length);

            fc.Write(arr, 0, (int)fr.Length);

繼續閱讀