類名: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 }
如果本文對你有所幫助,請打賞——1元就足夠感動我:)
聯系郵箱:[email protected]
我的GitHub:
https://github.com/vipstone關注公衆号:
作者:
王磊出處:
http://vipstone.cnblogs.com/本文版權歸作者和部落格園共有,歡迎轉載,請标明出處。