全局压缩http响应头

见代码:

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;
//}
}
}
[Compress]
public class BaseController: Controller
{
上一篇:voa 2015 / 4 / 26


下一篇:PHP正则表达式之定界符和原子介绍