MVC里的GZIP压缩
利用GZIP将数据压缩
先写个类:
public class CompressFilterAttribute : 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); } } }
在控制器里加个:
[CompressFilter] public ActionResult QJMap() { return View(); }