MVC 4 - JSON ajax 操作结果的 GZIP 压缩
MVC 4 - GZIP compression of JSON ajax action result
问题
我在 IIS 7.5 上的 MVC 4 应用程序 运行 上有一个 Telerik MVC UI 网格,它可能 return 大量 JSON 数据通过 AJAX,在极端情况下为 800kb 或更多。由于有效负载可能很大,我想对其进行 GZIP 压缩。对于我的一生,我无法让它工作。
控制器动作是:
public ActionResult _CustomBinding([DataSourceRequest] DataSourceRequest request, SearchMemberModel search)
{
//Do some stuff
return Json(result);
}
Fiddler 报告:
已经尝试过的
我已确保在 IIS 中启用了动态和静态压缩:
应用Web.Config修改:
<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>
我已经确定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" />
我试过 suggestion here serverRuntime frequentHitThreshold 需要修改。
有什么我遗漏的吗?
好的,看来我还需要在我的控制器中做一些事情:
根据以下摘自: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);
}
}
在控制器中的用法:
[Compress]
public class BookingController : BaseController
{...}
问题
我在 IIS 7.5 上的 MVC 4 应用程序 运行 上有一个 Telerik MVC UI 网格,它可能 return 大量 JSON 数据通过 AJAX,在极端情况下为 800kb 或更多。由于有效负载可能很大,我想对其进行 GZIP 压缩。对于我的一生,我无法让它工作。
控制器动作是:
public ActionResult _CustomBinding([DataSourceRequest] DataSourceRequest request, SearchMemberModel search)
{
//Do some stuff
return Json(result);
}
Fiddler 报告:
已经尝试过的
我已确保在 IIS 中启用了动态和静态压缩:
应用Web.Config修改:
<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>
我已经确定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" />
我试过 suggestion here serverRuntime frequentHitThreshold 需要修改。
有什么我遗漏的吗?
好的,看来我还需要在我的控制器中做一些事情:
根据以下摘自: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);
}
}
在控制器中的用法:
[Compress]
public class BookingController : BaseController
{...}