gpt4 book ai didi

xml - WCF 4.0 Rest 服务设置内容类型

转载 作者:数据小太阳 更新时间:2023-10-29 02:09:25 25 4
gpt4 key购买 nike

我刚刚完成我的第一个 WCF 4.0 Rest 服务,但不明白为什么返回数据的内容类型在通过 Fiddler 和 Firefox 调用服务之间会发生变化。这是我的服务契约(Contract):

[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 数据。至少那是我的意图。

当我调用 http://localhost:5050/ProjectServiceLibrary/project/27/xml 时在 Firefox 中,我可以看到内容类型设置为“text/html”,而在 fiddler 中调用的相同请求显示正确的内容类型“application/xml”。调用带有“/json”后缀的请求 - 在 firefox 中为“text/html”,在 fiddler 中为“application/json”。

那么,为什么会这样呢?我信任哪一个?我下载了 JSONView Firefox 附加组件,但这让一切看起来都像 json。它将 XML 视为 JSON。

我确定我遗漏了一些明显的东西。任何帮助将不胜感激。

最佳答案

这与客户端发送的请求中的Accept头有关。 Accept header 包含 MIME 类型的优先列表。接受 header 由客户端(Firefox、Fiddler)定义,并告诉服务器它能够接收哪些内容类型。服务器将根据优先级和兼容性使用最佳匹配。

FireFox 生成的接受 header 赋予 text/html 更高的优先级 - 告诉服务器在可能的情况下发送 text/html。您可能会发现 Fiddler 做的恰恰相反,给予 application/xml 更高的优先级 - 这解释了您所看到的。

Kris Jordans blog 上有关于请求 header 的更多详细信息.

关于xml - WCF 4.0 Rest 服务设置内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4822190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com