public class AuthorizeFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
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);
}
}
}
2. Js,Css In Global.asax.cs
void Application_PostReleaseRequestState(object sender, EventArgs e)
{
Compress_File(sender as HttpApplication);
}
public static void Compress_File(HttpApplication HttpApp)
{
string contentType = HttpApp.Response.ContentType;
if (contentType == "text/css" || contentType == "application/x-javascript" || contentType == "text/javascript")
{
HttpApp.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;
string acceptEncoding = HttpApp.Request.Headers["Accept-Encoding"];
if (acceptEncoding == null || acceptEncoding.Length == 0) return;
acceptEncoding = acceptEncoding.ToLower();
if (acceptEncoding.Contains("gzip"))
{
HttpApp.Response.AppendHeader("Content-encoding", "gzip");
HttpApp.Response.Filter = new GZipStream(HttpApp.Response.Filter, CompressionMode.Compress);
}
//else if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
//{
// HttpApp.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
// HttpApp.Response.AppendHeader("Content-Encoding", "deflate");
//}
}
}