Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 - GZIP compression of JSON ajax action result [duplicate]

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?

like image 932
MagicalArmchair Avatar asked Feb 27 '15 11:02

MagicalArmchair


1 Answers

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
{...}
like image 114
MagicalArmchair Avatar answered Nov 07 '22 01:11

MagicalArmchair