有什么办法避免呢?
跟这个问题差不多,http://q.cnblogs.com/q/44558/
看了,也运用了,还是没用。
我照着那上面的方法写,也没用,一如既往的出现乱码。我不知道GZIP流怎么解压缩啊?
使用new GZIPStream(response.Filter,CompressionMode.Decompress) 也不行。
public class CompressFilterAttribute : ActionFilterAttribute, IExceptionFilter { const CompressionMode compress = CompressionMode.Compress; const CompressionMode decompress = CompressionMode.Decompress; public override void OnActionExecuting(ActionExecutingContext filterContext) { object[] actionFilter = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoCompress), false); object[] controllerFilter = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(NoCompress), false); if (controllerFilter.Length == 1 || actionFilter.Length == 1) { return; } if (filterContext.HttpContext.Request.IsAjaxRequest()) { return; } HttpRequestBase request = filterContext.HttpContext.Request; HttpResponseBase response = filterContext.HttpContext.Response; string acceptEncoding = request.Headers["Accept-Encoding"]; if (acceptEncoding == null) return; if (acceptEncoding.ToLower().Contains("gzip")) { response.Filter = new GZipStream(response.Filter, compress); response.AppendHeader("Content-Encoding", "gzip"); } else if (acceptEncoding.ToLower().Contains("deflate")) { response.Filter = new DeflateStream(response.Filter, compress); response.AppendHeader("Content-Encoding", "deflate"); } } #endregion public void OnException(ExceptionContext filterContext) { var response = filterContext.HttpContext.Response; response.Filter = new GZipStream(response.Filter, decompress); } }
gzip在IIS设置就好了。为什么要写这段代码呢
有需求啊,有办法吗?