【发布时间】:2011-06-16 20:52:57
【问题描述】:
我刚刚完成了我的第一个 WCF 4.0 Rest 服务,但不明白为什么在通过 Fiddler 和 Firefox 调用服务之间返回的数据的 Content-Type 会发生变化。这是我的服务合同:
[ServiceContract]
public interface IProjectService
{
[OperationContract]
[WebGet(UriTemplate = "project/{id}/json", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
ProjectDataContract GetProjectJson(string id);
[OperationContract]
[WebGet(UriTemplate = "project/{id}/xml", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
ProjectDataContract GetProjectXml(string id);
[OperationContract]
[WebGet(UriTemplate = "userprojects/{userKey}/json", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<ProjectDataContract> GetProjectsByUserJson(string userKey);
[OperationContract]
[WebGet(UriTemplate = "userprojects/{userKey}/xml", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
List<ProjectDataContract> GetProjectsByUserXml(string userKey);
}
如您所见,我正在为每个操作设置响应格式。如果请求以“/json”结尾,那么我将返回 json 数据。如果请求以“/xml”结尾,则返回 xml 数据。至少我的意图是这样的。
当我在 Firefox 中调用 http://localhost:5050/ProjectServiceLibrary/project/27/xml 时,我可以看到内容类型设置为“text/html”,而在提琴手中调用的同一请求显示了正确的“应用程序/xml”内容类型。调用带有“/json”后缀的请求也会发生同样的情况——firefox 中的“text/html”和 fiddler 中的“application/json”。
那么,为什么会这样呢?我信任哪一个?我下载了 JSONView Firefox 插件,但这让一切看起来都像 json。它将 XML 视为 JSON。
我确定我遗漏了一些明显的东西。任何帮助将不胜感激。
【问题讨论】:
标签: xml wcf json content-type