使用jquery post对asp.net进行Gzip压缩设置

时间:2012-03-08 12:41:12

标签: jquery asp.net post compression gzip

在我的应用程序中,我做了一个jquery帖子,它获取了一个json对象数组。我需要在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>

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

1 个答案:

答案 0 :(得分:0)

错误是由web.config条目引起的。在IIS7.5中,将使用

相关问题