如何在HttpResponseMessage上设置内容编码

dib*_*487 6 c# encoding httpresponse asp.net-web-api

我想在HttpResponseMessage上设置Content-Encoding,我不能为我的生活找到方法.鉴于此WebApi动作

public HttpResponseMessage Get()
{
    byte[] tile = GetTile();

    var result = new HttpResponseMessage(HttpStatusCode.OK) {Content = new ByteArrayContent(tile)};

    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf");

    return result;
}
Run Code Online (Sandbox Code Playgroud)

如何将标头上的Content-Encoding设置为gzip?

 result.Content.Headers.ContentEncoding
Run Code Online (Sandbox Code Playgroud)

是只读的.

ric*_*lla 7

ContentEncoding属性是ICollection的一个实例.这提供了用于控制内容的添加和清除方法.


dib*_*487 5

不要贬低richzilla的答案,这当然是完全正确的并回答了我的问题。看到这得到了一些投票和访问,肯定还有其他人犯了和我一样的错误,所以值得说我的问题的完整答案是

result.Content.Headers.ContentEncoding.Add("gzip");
Run Code Online (Sandbox Code Playgroud)