从.NET 3.5 WCF Web服务返回JSON和XML格式(REST) [英] Returning JSON AND XML format from a .NET 3.5 WCF web service (REST)

查看:244
本文介绍了从.NET 3.5 WCF Web服务返回JSON和XML格式(REST)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回的XML响应现有的Web服务,我想补充一些返回JSON的新方法。我必须创建一个返回JSON单独的Web服务或我能有一个组合?

I have an existing web service that returns XML responses and I would like to add some new methods that return JSON. Do I have to create a separate web service that returns in JSON or can I have a mix?

如果我使用ResponseFormat = WebMessageFormat.JSON我需要有标注有[DataContractFormat],但我不能似乎有服务和[XmlSerializerFormat]这是必需的XML类型的响应格式。

If I use the ResponseFormat = WebMessageFormat.JSON I need to have the service annotated with [DataContractFormat] but I cant seem to have that and [XmlSerializerFormat] which is required for the xml type response format.

推荐答案

我不明白为什么这是不可能的。你用[的ServiceContract]属性(不DataContractFormat)标注的服务。它应该看起来像

I don't see why this isn't possible. You annotate the service with the [ServiceContract] attribute (not DataContractFormat). It should look like

 [ServiceContract]
    public interface IDoStuff
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
             UriTemplate = "DoStuff",
             ResponseFormat = WebMessageFormat.Json,
             RequestFormat = WebMessageFormat.Json)]
        TestObj DoWork(TestInputObj Inp);
    }

要使它XML,只是改变responseformat。当你做你的命令后,你会得到JSON,一个单独的方法与XML格式会给你的XML。

To make it xml, just change the responseformat. When you do your post command, you'll get json, a separate method with the xml format would give you xml.

这篇关于从.NET 3.5 WCF Web服务返回JSON和XML格式(REST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆