可能是我做错了什么,但我正在从用VS 2010构建的WCF Rest服务中返回XML。在fiddler中,您可以看到它将test/html作为内容类型返回。
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 222
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 16 Aug 2010 20:49:55 GMT因此,我继续在我的方法的webget属性上添加了以下内容,但是它仍然返回text/html .我假设我应该返回文本/XML的内容类型,因为我实际上是在返回XML?
下面是我的方法,我将ResponseFormat添加到属性中..。我不确定我是否需要体型(我不知道它能做什么,但我在一个例子中看到了它:-)
[WebGet(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
public List<SampleItem> GetCollection()
{
// TODO: Replace the current implementation to return a collection of SampleItem instances
return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
}无论如何,在项目更改和重建之后,它仍然返回错误的内容类型.我是不是失约了?
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 222
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 16 Aug 2010 20:54:15 GMT编辑
好的,我得到了一个可行的解决方案,但是属性方法没有任何影响,如果我把它放在这里,非常strange...but
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";现在我检查fiddler,内容类型实际上是text/xml。
但是我需要把它放在每个方法中,而属性方法似乎没有效果。
有人知道为什么吗?
发布于 2010-08-16 21:08:51
见A.
我想你想要。
OutgoingWebResponseContext context =
WebOperationContext.Current.OutgoingResponse;
context.ContentType = "image/jpeg"; ResponseFormat控制着其他东西。
https://stackoverflow.com/questions/3497263
复制相似问题