码迷,mamicode.com
首页 > 其他好文 > 详细

全局压缩http响应头

时间:2014-06-16 00:44:58      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

见代码:

bubuko.com,布布扣
public class CompressAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.IsChildAction)
                return;

            HttpRequestBase request = filterContext.HttpContext.Request;

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

            if (string.IsNullOrEmpty(acceptEncoding)) return;

            acceptEncoding = acceptEncoding.ToUpperInvariant();

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

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            //if (filterContext.Exception != null)
            //{
            //    filterContext.HttpContext.Response.Filter = null;
            //}
        }
    }
bubuko.com,布布扣
[Compress]
    public class BaseController: Controller
    {

 

全局压缩http响应头,布布扣,bubuko.com

全局压缩http响应头

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/yxlblogs/p/3784161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!