简体   繁体   English

REST WCF服务 - 差异数据输入格式(JSON / XML)

[英]REST WCF service - differences data input format (JSON/XML)

maybe this is a dumb question, but I need some help. 也许这是一个愚蠢的问题,但我需要一些帮助。

I'm using WCF to make a restful service. 我正在使用WCF来提供宁静的服务。 Users send me data to my methods through http post request. 用户通过http post请求将数据发送给我的方法。

I have done one method receiving a string representing data in json format. 我已经完成了一个接收表示json格式数据的字符串的方法。 So, I simply parse it and create my object to read. 所以,我只是解析它并创建我的对象来阅读。

My dumb question is: how can I set another method to be able to receive data input in XML format? 我的愚蠢问题是:我如何设置另一种方法来接收XML格式的数据输入? I mean, for json I simply expect a string to parse. 我的意思是,对于json我只想要一个字符串来解析。 For XML? 对于XML?

This is my first time with this issue and I'd like to learn how to do it in a clean way (like the string for json). 这是我第一次遇到这个问题而且我想学习如何以干净的方式(比如json的字符串)。

Can you help me? 你能帮助我吗?

UPDATE: For example, I have this sample method: 更新:例如,我有这个示例方法:

    [OperationContract]
    [WebInvoke(UriTemplate = "Patient/Add", Method = "POST")]
    int AddPatient(Patient patient);

I see the input is a custom class... so, I think clients can send me an xml representing this class.. or not? 我看到输入是一个自定义类...所以,我认为客户端可以发送一个代表这个类的xml ..或不? Can I simply manage the input like this? 我可以像这样管理输入吗?

Personally I use something like this. 我个人使用这样的东西。

    [OperationContract]
    [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "json")]
    void AddUsefulLinkJson(UsefulLinksWCF.Models.UsefulLink link);

    [OperationContract]
    [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml,
        UriTemplate = "xml")]
    void AddUsefulLinkXml(UsefulLinksWCF.Models.UsefulLink link);

So then when you use a client you can request data in json or xml like this: 那么当你使用客户端时,你可以像这样在json或xml中请求数据:

http://www.something.com/UsefulLinks/rest/xml http://www.something.com/UsefulLinks/rest/xml

or 要么

http://www.something.com/UsefulLinks/rest/json http://www.something.com/UsefulLinks/rest/json

There is a good article on MSDN regarding format selection starting from NET 4.0: 关于从.NET 4.0开始的格式选择,MSDN上有一篇很好的文章:

https://msdn.microsoft.com/en-us/library/ee476510%28v=vs.100%29.aspx https://msdn.microsoft.com/en-us/library/ee476510%28v=vs.100%29.aspx

When enabled, automatic formatting chooses the best format in which to return the response. 启用后,自动格式化将选择返回响应的最佳格式。 It determines the best format by checking the following, in order: 它通过按顺序检查以下内容来确定最佳格式:

The media types in the request message's Accept header. 请求消息的Accept标头中的媒体类型。

The content-type of the request message. 请求消息的内容类型。

The default format setting in the operation. 操作中的默认格式设置。

The default format setting in the WebHttpBehavior. WebHttpBehavior中的默认格式设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM