Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 在Nancy/Owin中处理gzip压缩_Asp.net_Owin_Nancy_Topshelf - Fatal编程技术网

Asp.net 在Nancy/Owin中处理gzip压缩

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

我们正在使用Nancy开发后端API。除了使用IIS托管它之外,我们还使用Owin自托管它。因为Owin不会自动处理gzip压缩(就像IIS那样),所以我们需要引入一个中间人来处理响应/请求压缩或反压缩。已经有图书馆了吗?如果没有可用的库,那么最好的方法是什么?

这里有一个指向开源项目的链接。
    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();
        };
    }