MVC 4 - GZIP compression of JSON ajax action result

13,950

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
{...}
Share:
13,950

Related videos on Youtube

MagicalArmchair
Author by

MagicalArmchair

Updated on October 01, 2022

Comments

  • MagicalArmchair
    MagicalArmchair over 1 year

    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?

  • Dina
    Dina almost 8 years
    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.
  • petrosmm
    petrosmm over 4 years
    That was the exact question I was going to ask, thank you @sep