public class GZipHelper
{
/// <summary>
/// Sets up the current page or handler to use GZip through a Response.Filter
/// IMPORTANT:
/// You have to call this method before any output is generated!
/// </summary>
public static void GZipEncodePage()
{
var response = HttpContext.Current.Response;
if (IsGZipSupported())
{
var acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (acceptEncoding.Contains("deflate"))
{
response.Filter = new DeflateStream(response.Filter,
CompressionMode.Compress);
response.AppendHeader("Content-Encoding", "deflate");
}
else
{
response.Filter = new GZipStream(response.Filter,
CompressionMode.Compress);
response.AppendHeader("Content-Encoding", "gzip");
}
}
// Allow proxy servers to cache encoded and unencoded versions separately
response.AppendHeader("Vary", "Content-Encoding");
}
/// <summary>
/// Determines if GZip is supported
/// </summary>
/// <returns></returns>
public static bool IsGZipSupported()
{
string acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(acceptEncoding)) return false;
if (!acceptEncoding.Contains("gzip") && !acceptEncoding.Contains("deflate")) return false;
var isPartial = HttpContext.Current.Request.Headers["x-microsoftajax"];
//Just checking to see if this is a partial page update
//See http://bit.ly/GlHfD for more info.
return System.String.Compare("Delta=true", isPartial, System.StringComparison.OrdinalIgnoreCase) != 0;
}
public static void CompressFile(string inputFilePath, string resultFilePath)
{
FileStream sourceFileStream = File.OpenRead(inputFilePath);
FileStream destFileStream = File.Create(resultFilePath);
GZipStream compressingStream = new GZipStream(destFileStream,
CompressionMode.Compress);
byte[] bytes = new byte[2048];
int bytesRead;
while ((bytesRead = sourceFileStream.Read(bytes, 0, bytes.Length)) != 0)
{
compressingStream.Write(bytes, 0, bytesRead);
}
sourceFileStream.Close();
compressingStream.Close();
destFileStream.Close();
}
}
custom GZip compression in ASP.NET MVC5
Running IIS without default compression. I'm trying to implement a custom Http Module, what I actually want is to implement gzip compression.
And add ref to
But it's not working.
Umbraco 7.2.6
Hi Shada,
We are using another approach. BasePage.cs:
GZipHelper.cs:
Thanks,
Alex
Are you using asp.net ?
Alex Skrypnyk, I use ASP.Net MVC5
Shada, you can configure compression through your web.config file as follows:
Thanks,
Alex
Alex Skrypnyk, unfortunately it did not help :(
Shada, what IIS do you use ?
How do you test gzip response or not ?
Best,
Alex
I see the response size and headers:
version iis 7
Shada, try this config section:
Nothing has changed
Edit:
Shada, it's really really strange. I think this:
Is useless key.
Thanks
Why? This adds a module in the iis 7
Now I try write "ActionFilter" for compressing data in my SurfaceController:
Use:
But the compression is still not happening! Below is response header in chrome.
Shada, what browser are you using ? maybe problem with browser ?
Alex Skrypnyk, I've tested in Chrome, Firefox and Fiddler
Shada, usually it's just config changes. Do you have some ideas what is the problem ?
Alex Skrypnyk, Maybe it has something to do with the CPU load. Although it is usually about 60%.
is working on a reply...