Web API 的压缩过滤器 [英] Compression filter for Web API

查看:32
本文介绍了Web API 的压缩过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的 MVC 操作使用压缩过滤器,详情如下:

I've been using the compression filter for my MVC actions as detailed here:

http://msdn.microsoft.com/en-us/magazine/gg232768.aspx

我尝试重新调整代码的用途,为 Web API 执行类似的操作,但遇到了障碍:

I've tried to re-purpose the code to do something similar for Web API, but I've hit a roadblock:

public class CompressAPIAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext)
    {
        var preferredEncoding = GetPreferredEncoding(filterContext.Request);
        Stream compressedStream = null;
        // Compress the response accordingly
        var response = filterContext.Response;
        response.Headers.Add("Content-encoding", preferredEncoding.ToString());

        if (preferredEncoding == CompressionScheme.Gzip)
        {
            response.Content = new GZipStream(compressedStream, CompressionMode.Compress); //THIS WON'T WORK
        } 

        if (preferredEncoding == CompressionScheme.Deflate)
        {
            response.Content = new DeflateStream(compressedStream, CompressionMode.Compress); //THIS WON'T WORK
        }
        return;
    }

    enum CompressionScheme
    {
        Gzip = 0,
        Deflate = 1,
        Identity = 2
    }

    private CompressionScheme GetPreferredEncoding(HttpRequestMessage request)
    {
        var acceptableEncoding = request.Headers.AcceptEncoding;

        if (acceptableEncoding.Where(h => h.Value.Contains("gzip")).Count() > 0)
            return CompressionScheme.Gzip;

        if (acceptableEncoding.Where(h => h.Value.Contains("deflate")).Count() > 0)
            return CompressionScheme.Deflate;

        return CompressionScheme.Identity;
    }

有什么想法可以将压缩流分配给响应的内容吗?

Any ideas how I can assign a compressed stream to the response's content?

我应该指出这是托管在 IIS 6.0 中的,我无法控制.

I should point out this is being hosted in IIS 6.0, which I do not control.

推荐答案

我认为你不应该在动作过滤器中这样做,因为模型绑定阶段发生在动作过滤器执行之前和模型绑定期间格式化程序可能正在读取流以反序列化它,在这种情况下它会失败.

I think you should not do this in an action filter as the modelbinding stage happens before action filters are executed and during modelbinding the formatters could be reading the stream to deserialize it, in which case it would fail.

如果您使用的是 IIS,请执行以下操作来设置压缩(以下摘自 Scott Hanselman 的 博文):

If you are using IIS, then do the following to setup compression(The following have some snippets from Scott Hanselman's blog post):

  • 在 IIS 中启用动态压缩"功能.

  • Enabled the "Dynamic Compression" feature in IIS.

返回 IIS 管理器,转到 SERVER 的页面,而不是 SITE.单击配置编辑器:

Back in IIS Manager, go to the page for the SERVER, not the SITE. Click on Configuration Editor:

从下拉菜单中选择 system.webServer/httpCompression:

From the dropdown, select system.webServer/httpCompression:

现在使用 Accept-Encoding 标头发出请求,您应该会看到预期的响应.

Now make requests using Accept-Encoding header and you should see the response as expected.

编辑(除了上面包括application/json; charset=utf-8"以涵盖两种json格式)

EDIT ( in addition to above include "application/json; charset=utf-8" to cover both json formats)

这篇关于Web API 的压缩过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆