zoukankan      html  css  js  c++  java
  • .NET MVC 简单实现GZIP

        GZIP的好处大家都知道,不过一般系统来说页面不会太大,很多人也没注意过这玩意儿。前段时间做一个系统用了一个国人开发的JQUERY富客户端框架DWZ(个人感觉这个框架还是蛮不错的),类似EXT的AJAX框架,框架的JS加上我自己的真不小。

       打算用GZIP来做压缩,之前在IIS6上做过,这个项目用MVC做,发现在.NET MVC下有更简单好用的办法来解决:写一个ActionFilter来实现GZIP,优点大家用了就知道,呵呵

        public class CompressAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
                if (!string.IsNullOrEmpty(acceptEncoding))
                {
                    acceptEncoding = acceptEncoding.ToLower();
                    var response = filterContext.HttpContext.Response;
                    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);
                    }
                }
            }
        }
    

    用法: 

            [Longin]
            [Compress]
            public ActionResult Index()
            {
                
                    return View();
            }
    

    只要在打算用GZIP压缩的Action上写上[Compress]就OK了,是不是很简单,呵呵~

  • 相关阅读:
    P2082 区间覆盖(加强版)
    Java基础
    @import
    POST方式"Content-type"是"application/x-www-form-urlencoded 的请求遇到的问题
    VBox 安装 macOS 10.12
    对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
    window.location 属性
    post get 区别
    vue 生命周期
    记事本
  • 原文地址:https://www.cnblogs.com/zhaojingjing/p/1940357.html
Copyright © 2011-2022 走看看