简体   繁体   English

如何在 HttpResponseMessage 上设置内容编码

[英]How do I set the content encoding on a HttpResponseMessage

I want to set the Content-Encoding on a HttpResponseMessage and I can't for the life of me find out how.我想在 HttpResponseMessage 上设置 Content-Encoding ,但我一生都无法找到方法。 Given this WebApi action鉴于此 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;
}

How do I set the Content-Encoding on the header to gzip?如何将标头上的 Content-Encoding 设置为 gzip?

 result.Content.Headers.ContentEncoding

is read only.是只读的。

The ContentEncoding property is an instance of ICollection . ContentEncoding属性是ICollection 的一个实例。

This provides .Add() and .Clear() methods for controlling the contents.这提供了用于控制内容的.Add().Clear()方法。

Not to detract from richzilla's answer which is of course totally correct and answered my question.不要贬低richzilla的答案,这当然是完全正确的并回答了我的问题。 Seeing as this is getting a few votes and visits there must be other people making the same mistake I did so it's worth saying the complete answer to my problem was看到这得到了一些投票和访问,肯定还有其他人犯了和我一样的错误,所以值得说我的问题的完整答案是

result.Content.Headers.ContentEncoding.Add("gzip");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM