Asp.net中Js、Css文件压缩辅助类
类名: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 }
关注下面二维码,订阅更多精彩内容。