Asp.net中Js、Css文件压缩辅助类
类名:WebCompressUtility.cs
代码如下:
view sourceprint?01 /// <summary>
02 /// Js、Css文件压缩辅助类
03 /// Stone_W
04 /// 2011.6.21
05 /// </summary>
06 public class WebCompressUtility
07 {
08 public WebCompressUtility() { }
09
10 #region 判断浏览器是否支持指定压缩
11 /// <summary>
12 /// 判断浏览器是否支持指定压缩
13 /// </summary>
14 /// <param name="ctype">文件压缩类型</param>
15 /// <param name="context">HttpContext对象</param>
16 /// <returns>bool</returns>
17 public static bool IsEncodingAccepted(CompressTypeEnum ctype, System.Web.HttpContext context)
18 {
19 if (null == context) return false;
20 return null != context.Request.Headers["Accept-Encoding"] && context.Request.Headers["Accept-Encoding"].Contains(ctype.ToString());
21 }
22 #endregion
23
24 #region 添加指定的压缩类型
25 /// <summary>
26 /// 添加指定的压缩类型
27 /// </summary>
28 /// <param name="ctype">文件压缩类型</param>
29 /// <param name="context">HttpContext对象</param>
30 public static void SetEncodingAccepted(CompressTypeEnum ctype, System.Web.HttpContext context)
31 {
32 if (null == context) return;
33 context.Response.AddHeader("Content-encoding", ctype.ToString());
34 }
35 #endregion
36
37 }
38
39 /// <summary>
40 /// 文件压缩类型
41 /// </summary>
42 /// example
43 public enum CompressTypeEnum
44 {
45 gzip,
46 deflate
47 }
调用:
1.WebCompressUtility.IsEncodingAccepted(CompressTypeEnum.gzip, context);
2.WebCompressUtility.SetEncodingAccepted(CompressTypeEnum.gzip, context);