How to Implement Gzip Compression in ASP.NET

How to implement GZip compression in ASP.NET?

For compressing JS & CSS files you actually have to handle that at the IIS level, since these files are rendered directly without the ASP.NET runtime.

You could make a JSX & CSSX extension mapping in IIS to the aspnet_isapi.dll and then take advantage of your zip code, but IIS is likely to do a better job of this for you.

The content-encoding header tells the browser that it needs to unzip the content before rendering. Some browsers are smart enough to figure this out anyway, based on the shape of the content, but it's better to just tell it.

The Accept-encoding cache setting is there so that a cached version of the gzipped content won't be sent to a browser that requested only text/html.

Gzip compression asp.net c#

Looks like you want to add the Response.Filter, see below.

private void writeBytes()
{
var response = this.context.Response;
bool canGzip = true;

if (canGzip)
{
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
}
else
{
response.AppendHeader("Content-Encoding", "utf-8");
}

response.AppendHeader("Content-Length", this.responseBytes.Length.ToString());
response.ContentType = this.isScript ? "text/javascript" : "text/css";
response.ContentEncoding = Encoding.Unicode;
response.OutputStream.Write(this.responseBytes, 0, this.responseBytes.Length);
response.Flush();
}

}

Gzip compression not working ASP.net MVC5

If you can't control IIS, just add this to your Global.ASCX. tested on Android, iPhone, and most PC browsers.

 protected void Application_BeginRequest(object sender, EventArgs e)
{

// Implement HTTP compression
HttpApplication app = (HttpApplication)sender;

// Retrieve accepted encodings
string encodings = app.Request.Headers.Get("Accept-Encoding");
if (encodings != null)
{
// Check the browser accepts deflate or gzip (deflate takes preference)
encodings = encodings.ToLower();
if (encodings.Contains("deflate"))
{
app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
}
else if (encodings.Contains("gzip"))
{
app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
}
}
}

How do I enable GZIP compression for POST (upload) requests to a SOAP WebService on IIS 7?

I've blogged on that 4 years ago

http://netpl.blogspot.com/2009/07/aspnet-webservices-two-way-response-and.html

I wonder why you haven't found this by googling. Anyway, this should work for you, we have it working in a production environment for 4 years now.



Related Topics



Leave a reply



Submit