GZipModule : IHttpModule ASP.NET的問題
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.IO.Compression;
namespace ToToGaGa.net
{
public class GZipModule : IHttpModule
{
void IHttpModule.Dispose()
{
//throw new Exception("The method or operation is not implemented.");
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string encodings = app.Request.Headers.Get("Accept-Encoding");
if (encodings == null)
return;
Stream s = app.Response.Filter;
encodings = encodings.ToLower();
if (encodings.Contains("gzip"))
{
app.Response.Filter = new GZipStream(s, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
app.Context.Trace.Warn("GZIP Compression on");
}
else
{
app.Response.Filter = new DeflateStream(s, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
app.Context.Trace.Warn("Deflate Compression on");
}
}
}
}
<httpModules>
<add name="GZipModule" type="ToToGaGa.net.GZipModule, ToToGaGa.net" />
</httpModules>
以上功能可實現GZip壓縮,正常,但就是把我的圖片都相反壓大,如何過濾所有image/jpg/png/gif等等圖片不壓縮??