对Web API融为一体pression过滤器 [英] Compression filter for Web API

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

问题描述

我一直使用的COM pression过滤器对我的MVC的行动如下详述:

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

<一个href=\"http://msdn.microsoft.com/en-us/magazine/gg232768.aspx\">http://msdn.microsoft.com/en-us/magazine/gg232768.aspx

我试图重新目的code做的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;
    }

任何想法如何,我可以分配pssed流响应的内容的COM $ P $?

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.

推荐答案

我觉得你应该的的做一个动作过滤器操作筛选器执行前的modelbinding阶段发生,modelbinding在格式化过程中可以读取该流至反序列化,在这种情况下,它会失败。

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,然后执行以下步骤设置COM pression(以下具有斯科特Hanselman的一些片段<一个href=\"http://www.hanselman.com/blog/EnablingDynamicCom$p$pssionGzipDeflateForWCFDataFeedsODataAndOtherCustomServicesInIIS7.aspx\">blog帖子):

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


  • 在IIS中启用了动态的COM pression功能。

  • Enabled the "Dynamic Compression" feature in IIS.

早在IIS管理器,转到页的服务器,不是网站。点击配置编辑器:

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

从下拉列表中,选择system.webServer / httpCom pression:

From the dropdown, select system.webServer/httpCompression:

现在让使用的Accept-Encoding 头,你应该看到如预期的响应请求。

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

修改(适用于除上述包括应用/ JSON的;字符集= UTF-8,既包括JSON格式)

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

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

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