Asp.net 在Nancy/Owin中处理gzip压缩
我们正在使用Nancy开发后端API。除了使用IIS托管它之外,我们还使用Owin自托管它。因为Owin不会自动处理gzip压缩(就像IIS那样),所以我们需要引入一个中间人来处理响应/请求压缩或反压缩。已经有图书馆了吗?如果没有可用的库,那么最好的方法是什么?这里有一个指向开源项目的链接。Asp.net 在Nancy/Owin中处理gzip压缩,asp.net,owin,nancy,topshelf,Asp.net,Owin,Nancy,Topshelf,我们正在使用Nancy开发后端API。除了使用IIS托管它之外,我们还使用Owin自托管它。因为Owin不会自动处理gzip压缩(就像IIS那样),所以我们需要引入一个中间人来处理响应/请求压缩或反压缩。已经有图书馆了吗?如果没有可用的库,那么最好的方法是什么?这里有一个指向开源项目的链接。 using System.IO.Compression; protected override void ApplicationStartup(TinyIoCContainer contain
using System.IO.Compression;
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
pipelines.AfterRequest.AddItemToEndOfPipeline(AddGZip);
}
private void AddGZip(NancyContext context)
{
if ((!context.Response.ContentType.Contains("application/json")) || !context.Request.Headers.AcceptEncoding.Any(
x => x.Contains("gzip")) || !CompressResponse(context.Request.Url.ToString())) return;
var jsonData = new MemoryStream();
context.Response.Contents.Invoke(jsonData);
jsonData.Position = 0;
context.Response.Headers["Content-Encoding"] = "gzip";
context.Response.Contents = s =>
{
var gzip = new GZipStream(s, CompressionMode.Compress, true);
jsonData.CopyTo(gzip);
gzip.Close();
};
}