不能设置的Htt presponseMessage头Content-Type头? [英] Can't set Content-Type header on HttpResponseMessage headers?
问题描述
我使用ASP.NET的WebAPI创建一个RESTful API。我在我的控制器之一中创建一个PUT方法和code是这样的:
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:
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的浏览器的位置,它给了我这样的异常:
When I PUT to that location with the browser via AJAX, it gives me this Exception:
误用头名。确保请求头与使用
HTT prequestMessage,响应头与HTT presponseMessage和
内容头部与HttpContent对象。
Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
但不是内容类型
的回应是非常有效的头?为什么我会收到此异常?
But isn't Content-Type
a perfectly valid header for a response? Why am I getting this 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.
}
这篇关于不能设置的Htt presponseMessage头Content-Type头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!