JSON 和 XML 同时用于 WCF 的 ResponseFormat? [英] ResponseFormat for WCF in JSON and XML at the same time?

查看:31
本文介绍了JSON 和 XML 同时用于 WCF 的 ResponseFormat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如使用 REST 请求对象时,是否可以获取 json 和 xml 格式的响应,或者我是否必须创建类似于以下内容的 UriTemplates:

When requesting an object for example using REST, is it possible to get the response in json and xml format or do I have to create UriTemplates that are something like:

[WebInvoke(UriTemplate="&format=json?user/{id}", ResponseFormat=WebMessageFormat.Json)]

[WebInvoke(UriTemplate="&format=xml?user/{id}", ResponseFormat=WebMessageFormat.Xml)]

我之所以这么问是因为我可能需要为 Android 手机上的应用返回一种格式,而为笔记本电脑上的应用返回另一种类型.

The reason I ask is because I may need one format returned for an app on an android phone for example and another type returned for an app on a laptop.

另外,这些方法是否可以具有相同的名称,例如 Register 或者我是否必须调用一个名称:

Also, Can the methods have the same name such as Register or do I have to have one called:

RegisterJSON(User user) 和另一个叫 RegisterXML(User user)

RegisterJSON(User user) and another called RegisterXML(User user)

推荐答案

可以在 WCF 4 (.NET 4.0).检查 WebHttpBehavior 的 AutomaticFormatSelectionEnabled 属性.您还可以从配置中设置此属性.我在此处展示了示例.

It is available out of the box in WCF 4 (.NET 4.0). Check AutomaticFormatSelectionEnabled property of WebHttpBehavior. You can also set this property from configuration. I showed the example here.

这篇关于JSON 和 XML 同时用于 WCF 的 ResponseFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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