类名:WebCompressUtility.cs
代码如下:
/// <summary> /// Js、Css文件压缩辅助类 /// Stone_W /// 2011.6.21 /// </summary> public class WebCompressUtility { public WebCompressUtility() { } #region 判断浏览器是否支持指定压缩 /// <summary> /// 判断浏览器是否支持指定压缩 /// </summary> /// <param name="ctype">文件压缩类型</param> /// <param name="context">HttpContext对象</param> /// <returns>bool</returns> public static bool IsEncodingAccepted(CompressTypeEnum ctype, System.Web.HttpContext context) { if (null == context) return false; return null != context.Request.Headers["Accept-Encoding"] && context.Request.Headers["Accept-Encoding"].Contains(ctype.ToString()); } #endregion #region 添加指定的压缩类型 /// <summary> /// 添加指定的压缩类型 /// </summary> /// <param name="ctype">文件压缩类型</param> /// <param name="context">HttpContext对象</param> public static void SetEncodingAccepted(CompressTypeEnum ctype, System.Web.HttpContext context) { if (null == context) return; context.Response.AddHeader("Content-encoding", ctype.ToString()); } #endregion } /// <summary> /// 文件压缩类型 /// </summary> /// example public enum CompressTypeEnum { gzip, deflate }
调用:
1.WebCompressUtility.IsEncodingAccepted(CompressTypeEnum.gzip, context);
2.WebCompressUtility.SetEncodingAccepted(CompressTypeEnum.gzip, context);
示例:
1
if
(WebCompressUtility.IsEncodingAccepted(CompressTypeEnum.gzip, context))
2 {
3 WebCompressUtility.SetEncodingAccepted(CompressTypeEnum.gzip, context);
4 }
2 {
3 WebCompressUtility.SetEncodingAccepted(CompressTypeEnum.gzip, context);
4 }
如果本文对你有所帮助,请打赏——1元就足够感动我:)
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
关注公众号:
作者: 王磊
出处: http://vipstone.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,请标明出处。