我正在使用 ASP.NET WebApi。我正在我的一个控制器中创建一个 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
一个完全有效的响应头?为什么我会收到此异常?
看看HttpContentHeaders.ContentType 属性 http://msdn.microsoft.com/en-us/library/system.net.http.headers.httpcontentheaders.contenttype.aspx:
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.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)