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
Executenonquery for Select SQL Statement Returning No Rows
Does Entity Framework 4 Code First Have Support for Identity Generators Like Nhibernate
Can't Change Gameobject Color via Script
Token Based Authentication in ASP.NET Core (Refreshed)
How to Generate a Hashcode from a Byte Array in C#
Cast Linq Result to Observablecollection
How to Read Single Excel Cell Value
How to Effectively Draw on Desktop in C#
How to Add 'Comments' to a Jpeg File Using C#
Calculate the Display Width of a String in C#
Does Garbage Collection Run During Debug
Get Text/Value from Textbox After Value/Text Changed Server Side
Can You Explain Liskov Substitution Principle with a Good C# Example
How to Add a Line Break in C# .Net Documentation
Button Inside a Winforms Textbox
How to Use a Controller in Another Assembly in ASP.NET Core MVC 2.0
How to Load Assemblies Located in a Folder in .Net Core Console App