Type header on HttpResponseMessage headers?
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;
}
When I PUT to that location with the browser via AJAX, it gives me this Exception:
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.
}
链接地址: http://www.djcxy.com/p/46252.html
上一篇: 内容标题删除字符串授权失败