我有一个返回 XML 响应的现有 Web 服务,我想添加一些返回 JSON 的新方法。我是否必须创建一个以 JSON 格式返回的单独 Web 服务,或者我可以混合使用吗?
如果我使用 ResponseFormat = WebMessageFormat.JSON,我需要使用 [DataContractFormat] 对服务进行注释,但我似乎无法使用 xml 类型响应格式所需的 [XmlSerializerFormat]。
最佳答案
我不明白为什么这是不可能的。您使用 [ServiceContract] 属性(而不是 DataContractFormat)注释服务。它应该看起来像
[ServiceContract]
public interface IDoStuff
{
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "DoStuff",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
TestObj DoWork(TestInputObj Inp);
}
要使其成为 xml,只需更改响应格式即可。当你执行你的 post 命令时,你会得到 json,一个单独的 xml 格式的方法会给你 xml。
关于.net - 从 .NET 3.5 WCF Web 服务 (REST) 返回 JSON 和 XML 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1140460/