【发布时间】:2011-06-26 23:09:29
【问题描述】:
我正在实现 HttpModule 来压缩请求。 下面是 HttpModule 的代码:
public class Global : IHttpModule
{
public void Init(HttpApplication app)
{
app.PostReleaseRequestState += new EventHandler(app_PostReleaseRequestState);
}
void app_PostReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
// If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
if (acceptEncoding.Contains("gzip"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.Contains("deflate"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "deflate");
}
}
它能够拦截和压缩开发 Web 服务器中的 js 和 css,但是当我从 IIS 5.1 运行它时 它无法压缩 js 和 css 文件。 请帮忙。
【问题讨论】:
标签: c# asp.net httpmodule