日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

谈mvc开发中gzip压缩的应用

發(fā)布時間:2025/7/14 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 谈mvc开发中gzip压缩的应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

壓縮view的內(nèi)容,可加過濾器

?

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);}}}

然后在要壓縮的頁面控制器上加標(biāo)簽。

?

[GzipFilter]public ActionResult Index()

?

現(xiàn)在基本上所有的瀏覽器支持gzip, deflate.

這里是編程對css和js文件進(jìn)行壓縮放在本地,然后發(fā)送給客戶端。

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

----關(guān)鍵是請求,只對action有效,像js,css文件的請求,在BeginRequest里檢測不到。這種方法運(yùn)行在iis7里很完美,文件大概會被壓縮到原來的1/3到1/4.

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

如果有誰找到iis6里面可以運(yùn)行的方法麻煩告訴我,或許能一起討論找到更好的解決方案,非常感謝!

---pukuimin@qq.com

瀏覽器檢測到這個頭,就會對文件進(jìn)行解壓縮,就正常運(yùn)行了。

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")){FileInfo fi = new FileInfo(mapfilepath);Response.AppendHeader("Content-Encoding", "gzip");int len = mapfilepath.Length - "gzip.js".Length;if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);}}#endregion}else if (acceptEncoding.Contains("deflate")){#region deflate處理if (filepath.EndsWith(".css"))//css文件處理 {Response.AppendHeader("Content-Type", "text/css");Request.ContentType = "text/css";if (filepath.EndsWith("deflate.css")){FileInfo fi = new FileInfo(mapfilepath);Response.AppendHeader("Content-Encoding", "gzip");int len = mapfilepath.Length - "deflate.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("deflate.js")){FileInfo fi = new FileInfo(mapfilepath);Response.AppendHeader("Content-Encoding", "gzip");int len = mapfilepath.Length - "deflate.js".Length;if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);}}#endregion}}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){//準(zhǔn)備輸入輸出文件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);gzs.Close();fr.Close();fc.Close();}

?

總結(jié)

以上是生活随笔為你收集整理的谈mvc开发中gzip压缩的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。