C# 无法在 HttpResponseMessage 标头上设置 Content-Type 标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13377957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't set Content-Type header on HttpResponseMessage headers?
提问by Jez
I'm using the ASP.NET WebApi to create a RESTful API. I'm creating a PUT method within one of my controllers, and the code looks like this:
我正在使用 ASP.NET WebApi 创建一个 RESTful API。我正在我的一个控制器中创建一个 PUT 方法,代码如下所示:
public HttpResponseMessage Put(int idAssessment, int idCaseStudy, string value) {
var response = Request.CreateResponse();
if (!response.Headers.Contains("Content-Type")) {
response.Headers.Add("Content-Type", "text/plain");
}
response.StatusCode = HttpStatusCode.OK;
return response;
}
When I PUT to that location with the browser via AJAX, it gives me this Exception:
当我通过 AJAX 使用浏览器放置到该位置时,它给了我这个异常:
Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
误用的标题名称。确保请求标头与 HttpRequestMessage 一起使用,响应标头与 HttpResponseMessage 一起使用,内容标头与 HttpContent 对象一起使用。
But isn't Content-Typea perfectly valid header for a response? Why am I getting this exception?
但不是Content-Type一个完全有效的响应标头吗?为什么我会收到此异常?
采纳答案by dtb
Have a look at the HttpContentHeaders.ContentType Property:
看看HttpContentHeaders.ContentType 属性:
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
if (response.Content == null)
{
response.Content = new StringContent("");
// The media type for the StringContent created defaults to text/plain.
}
回答by SandRock
Something is missing in ASP Web API: the EmptyContenttype. It will allow sending an empty body while still allowing all content-specific headers.
ASP Web API 中缺少一些东西:EmptyContent类型。它将允许发送空正文,同时仍允许所有特定于内容的标头。
Put the following class somewhere in your code :
将以下类放在代码中的某处:
public class EmptyContent : HttpContent
{
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return Task.CompletedTask;
}
protected override bool TryComputeLength(out long length)
{
length = 0L;
return true;
}
}
Then use it as you wish. You now have a content object for your extra headers.
然后随心所欲地使用它。您现在有一个用于额外标题的内容对象。
response.Content = new EmptyContent();
response.Content.Headers.LastModified = file.DateUpdatedUtc;
Why use EmptyContentinstead of new StringContent(string.Empty)?
为什么使用EmptyContent而不是new StringContent(string.Empty)?
StringContentis a heavy class that executes lots of codes (because it inheritsByteArrayContent)- so let's save a few nanoseconds
StringContentwill add an extra useless/problematic header:Content-Type: plain/text; charset=...- so let's save a few network bytes
StringContent是一个执行大量代码的重类(因为它继承了ByteArrayContent)- 所以让我们节省几纳秒
StringContent将添加一个额外的无用/有问题的标题:Content-Type: plain/text; charset=...- 所以让我们节省一些网络字节

![C# ConfigurationManager.AppSettings[Key] 是否每次都从 web.config 文件中读取?](/res/img/loading.gif)