[英]Gzip compression setting for asp.net with jquery post
在我的应用程序中,我做了一个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>
我需要进行其他配置更改吗?
错误是由于web.config条目引起的。 在IIS7.5中,将使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.