mar*_*ith 6 rest wcf visual-studio-2010
可能是我做错了,但我从我的WCF Rest服务返回XML,这是用VS 2010构建的.在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
Run Code Online (Sandbox Code Playgroud)
所以我继续在我的方法的webget属性上添加以下内容但它仍然返回text/html ...我假设我应该返回text/xml的内容类型,因为我实际上是在返回XML?
继承我的方法,我将ResponseFormat添加到属性...我不确定我是否需要bodystyle(我不知道它做了什么,但在一个例子中看到它:-))
[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" } };
}
Run Code Online (Sandbox Code Playgroud)
无论如何,在改变和重建项目之后,它仍然会返回错误的内容类型......我是否错过了什么?
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
Run Code Online (Sandbox Code Playgroud)
编辑
好吧,我有一个工作的解决方案,但属性方法没有效果,非常奇怪......但如果我把它
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
Run Code Online (Sandbox Code Playgroud)
现在我检查fiddler,内容类型实际上是text/xml.
但我需要把它放在每个方法中,属性方法似乎没有效果.
谁知道为什么?
根据这一点,Firefox请求标头对text/html的优先级高于text/xml,导致用xml或json修饰的WCF服务方法返回"错误"响应,尽管我可以想象这是正确的行为.
您可以通过显式设置强制响应内容类型
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
Run Code Online (Sandbox Code Playgroud)
或同等学历.我想这是唯一的选择,如果你真的想强制所有浏览器/客户端的特定内容类型响应.
参见例如
我想你想要例如
OutgoingWebResponseContext context =
WebOperationContext.Current.OutgoingResponse;
context.ContentType = "image/jpeg";
Run Code Online (Sandbox Code Playgroud)
ResponseFormat控制其他东西。
| 归档时间: |
|
| 查看次数: |
6290 次 |
| 最近记录: |