Reputation: 13447
I want to zipping response in my Asp.Net
web site. and I wrote this code:
public static void CompressPage(HttpRequest Request, HttpResponse Response)
{
string acceptEncoding = Request.Headers["Accept-Encoding"];
Stream prevUncompressedStream = Response.Filter;
if (acceptEncoding.IsEmpty())
{
return;
}
acceptEncoding = acceptEncoding.ToLower();
if (acceptEncoding.Contains("gzip"))
{
Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.Contains("deflate"))
{
Response.Filter = new DeflateStream(prevUncompressedStream, CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "deflate");
}
}
and call it it Page_Load
event:
protected void Page_Load(object sender, EventArgs e)
{
...
ZipHtmlPage.CompressPage(Request, Response);
}
The problem is when I run the code with and without above code in Page_Load
the size of response does not change.
Where is the problem?
Thanks
Edit 1)
I think that "Content-Encoding", "gzip"
doesn't add to header:
I don't know why?
Edit 2)
When I use HttpModule
for doing http compression:
public class CompressModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
}
public void Dispose()
{
}
}
I got this in every pages:
Upvotes: 5
Views: 1212
Reputation: 592
I think it's non-standard how you are adding your compression. What I've done in the past is configured GZip in the startup file. What else I've noticed is the compression didn't take effect in debug mode. Try deploying or running your app in release mode to see if it compresses the response.
In my configure services method of startup I have this:
services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Optimal;
});
Upvotes: 1
Reputation: 6073
This is not the correct way to compress pages like this, first, you need to check if the browser accepts gzip
or deflate
then start compressing it, that's why you are seeing wired characters.
private void Application_BeginRequest(Object source, EventArgs e)
{
// wrong and dangerous
HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
}
The better way:
void context_BeginRequest(object sender, EventArgs e)
{
var app = (HttpApplication)sender;
var encodings = app.Request.Headers.Get("Accept-Encoding");
if (encodings == null)
return;
var baseStream = app.Response.Filter;
// check if browser accepts gzip or deflate
if (encodings.ToLower().Contains("gzip"))
{
app.Response.Filter = new GZipStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
app.Context.Trace.Warn("GZIP compression on");
}
else if (encodings.ToLower().Contains("deflate"))
{
app.Response.Filter = new DeflateStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
app.Context.Trace.Warn("Deflate compression on");
}
}
I recommend using a library called HttpCompress
, you can access it here. And again as other people said why don't you use built-in IIS compression module?
Upvotes: 0
Reputation: 387
do you add these codes to your web.config file (for css and javascript files):
<system.webServer>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
...
maybe this link help you How to implement GZip compression in ASP.NET?
Upvotes: 1