【问题标题】:Gzip compression setting for asp.net with jquery post带有 jquery post 的 asp.net 的 Gzip 压缩设置
【发布时间】:2012-03-08 12:41:11
【问题描述】:

在我的应用程序中,我做了一个获取 json 对象数组的 jquery 帖子。我需要在 iis7.5 和 json 上进行哪些设置?

我做了一个自定义的httpmodule

private void Compress(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpRequest request = app.Request;
            HttpResponse response = app.Response;

            //Ajax Web Service request is always starts with application/json
            if (request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith("application/json"))
            {
                //User may be using an older version of IE which does not support compression, so skip those
                if (!((request.Browser.IsBrowser("IE")) && (request.Browser.MajorVersion <= 6)))
                {
                    string acceptEncoding = request.Headers["Accept-Encoding"];

                    if (!string.IsNullOrEmpty(acceptEncoding))
                    {
                        acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture);

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

在 jquery 帖子中,我添加了

$.ajax({
    type: "POST", //GET or POST or PUT or DELETE verb
    url: newGrid.Serviceurl, // Location of the service
    data: JSON.stringify(newGrid), //Data sent to server
    contentType: newGrid.contenttype, // content type sent to server
    dataType: "json", //Expected data format from server
    processdata: true, //True or False
    beforeSend: function (request) {
        request.setRequestHeader("Accept-Encoding", "gzip");
    },
    success: function (result) { // get an array of json objects 
        alert('done');
    }
 });

web.config 更改 -

<httpModules>
  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  <add name="GzipCompression" type="GzipCompression"/>
</httpModules>

我是否需要进行任何其他配置更改。

【问题讨论】:

  • 从你向浏览器询问“Accept-Encoding”的那一刻起,就不需要对IE进行检查了。

标签: jquery asp.net post compression gzip


【解决方案1】:

错误是由于 web.config 条目造成的。在IIS7.5中,会用到

【讨论】:

    猜你喜欢
    • 2010-11-22
    • 2012-09-24
    • 1970-01-01
    • 2012-09-19
    • 2021-07-28
    • 2011-04-05
    • 1970-01-01
    • 2014-05-31
    • 2015-07-14
    相关资源
    最近更新 更多