无法在HttpResponseMessage标头上设置Content-Type标头?

我正在使用ASP.NET WebApi来创build一个RESTful API。 我在我的一个控制器中创build一个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; } 

当我通过AJAX的浏览器放到那个位置,它给了我这个例外:

错误的标题名称。 确保请求头与HttpRequestMessage一起使用,使用HttpResponseMessage响应头和使用HttpContent对象的内容头。

但是不是Content-Type一个完全有效的响应头? 为什么我得到这个exception?

看看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. }