14

The problem

I have a Telerik MVC UI grid on an MVC 4 app running on IIS 7.5 that can potentially return a large amount of JSON data via AJAX, in extreme cases 800kb or more. As the payload can be large, I want to GZIP it. For the life of me, I cannot get it working.

The controller action is:

public ActionResult _CustomBinding([DataSourceRequest] DataSourceRequest request, SearchMemberModel search)
{
    //Do some stuff

   return Json(result);
}

Fiddler reports: enter image description here

What has been tried

I have ensured dynamic and static compression is enabled in IIS:

enter image description here

App Web.Config amended:

  <system.webServer>
    <serverRuntime frequentHitThreshold="1" frequentHitTimePeriod="10:00:00" />

    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="false">
      <remove name="FormsAuthentication" />
    </modules>

    <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>

    <urlCompression doStaticCompression="true" doDynamicCompression="true" />

  </system.webServer>

I've made sure the ApplicationHost file has the right mime types:

    <add mimeType="application/json" enabled="true" />
    <add mimeType="application/json; charset=utf-8" enabled="true" />
    <add mimeType="application/json;charset=utf-8" enabled="true" />

I've tried the suggestion here that the serverRuntime frequentHitThreshold needs amending.

Is there something I'm missing?

Community
  • 1
  • 1
MagicalArmchair
  • 911
  • 1
  • 12
  • 26
  • But how to compress image mainly in png, considering the browser requests http://10.0.0.10:70/Maps/FirstFloor.jpg. where `Maps` is a folder – Mahdi Alkhatib Aug 28 '17 at 12:17
  • Also I have a question, if I had my code on development machine and images reside on a local server, would it affect the loading time while developing and testing the application?? – Mahdi Alkhatib Aug 28 '17 at 12:18

1 Answers1

25

Okay, so it would seem I need to do something in my controller also:

As per the below extracted from: how to gzip content in asp.net MVC?

public class CompressAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
        if (string.IsNullOrEmpty(encodingsAccepted)) return;

        encodingsAccepted = encodingsAccepted.ToLowerInvariant();
        var response = filterContext.HttpContext.Response;

        if (encodingsAccepted.Contains("deflate"))
        {
            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
        }
        else if (encodingsAccepted.Contains("gzip"))
        {
            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        }
    }

usage in controller:

[Compress]
public class BookingController : BaseController
{...}
Community
  • 1
  • 1
MagicalArmchair
  • 911
  • 1
  • 12
  • 26
  • 2
    Nice answer! some test systems use "foxy proxy" to record test scenarios and it has problem with deflate compression. In this case, it's better to favor gzip to deflate by reverting the last two if statements. – Dina Jun 02 '16 at 06:29
  • That was the exact question I was going to ask, thank you @sep – petrosmm Jan 23 '20 at 20:08