public ActionResult Index() { if (Request.IsAjaxRequest()) { return Json(new {/* */}, JsonRequestBehavior.AllowGet); } return View(); }
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> <script type="text/javascript"> jQuery(function ($) { $.ajax({ url: '@Url.Action("Index")' }); }) </script>
X-Requested-With: XMLHttpRequest
ajax request is coming and the browser reports that it understands gzip or deflate with the Accept-Encoding: gzip, deflate
header, to which the server responds with json Content-Type: application/json
, but no compression, because there is no Content-Encoding
header.web.config
<system.webServer> <urlCompression doStaticCompression="true" doDynamicCompression="true" /> </system.webServer>
Content-Encoding
in the response header, and does not. <system.webServer> <httpCompression> <dynamicTypes> <add mimeType="application/json" enabled="true" /> <add mimeType="application/json; charset=utf-8" enabled="true" /> </dynamicTypes> </httpCompression> </system.webServer>
Content-Encoding: gzip
. But there is one very big BUT. httpCompression
section can only be defined in applicationhost.config
. And this means that either you are an IIS administrator, or this option does not suit you (unless, of course, you convince your hoster of the opposite).text/javascript
. Of course, this is more like a hack, but it's better than nothing. public ActionResult Index() { if (Request.IsAjaxRequest()) { return new JsonResult() { ContentType = "text/javascript", ContentEncoding = Encoding.UTF8, JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new {/* */} }; } return View(); }
$.ajax
function, we immediately get an error of data parsing. In other words, we must now use either $.getJSON
or explicitly indicate that we are expecting json
. <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> <script type="text/javascript"> jQuery(function ($) { $.ajax({ url: '@Url.Action("Index")', dataType: 'json', }); }) </script>
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); } } }
CompressionLevel
. Maybe one of you will tell? I consider this moment important for me, because depending on the project, the volume of transmitted traffic is sometimes important, and sometimes, on the contrary, processor time.Source: https://habr.com/ru/post/180769/
All Articles