简体   繁体   English

WCF REST POST XML

[英]WCF REST POST XML

Here is a code snippet. 这是一个代码片段。 Please tell me whats the difference between these two codes and also which content suitable for these code snippets. 请告诉我这两个代码之间的区别是什么,以及适合这些代码段的内容。 "application/xml" or "plain/text" “应用程序/ xml”或“纯文本/文本”

[OperationContract]
[WebInvoke(Method="POST", UriTemplate="DoSomething")]
public XElement DoSomething(XElement body) {
    ...
    return new XElement("Result");
}

[OperationContract]
[WebInvoke(Method="POST", UriTemplate="DoSomething")]
public string DoSomething(string body) {
    ...
    return "thanks";
}

WCF thinks everything by default is XML so both endpoints will return XML. WCF认为默认情况下所有内容都是XML,因此两个端点都将返回XML。 The second one will return 第二个将返回

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">thanks</string>

With the content type application/xml. 使用内容类型application / xml。 And if you want POST a string to it, you will have to send it a XML serialized string. 而且,如果您要向其发布字符串,则必须向其发送XML序列化的字符串。 Goofy isn't it. 高飞不是吗。

If you really want to return just a string, then use Stream as your return type. 如果您确实只想返回一个字符串,则使用Stream作为您的返回类型。 Or take a look at WCF in .Net 4. It looks like they made it a whole lot easier to return other types. 或看看.Net 4中的WCF。看起来它们使返回其他类型变得容易得多。

Both methods respond to a POST request on a URI of the format '{BASE_URI}/DoSomething' (just a guess) 两种方法都对格式为“ {BASE_URI} / DoSomething”的URI响应POST请求(只是一个猜测)

  • The first one expects some XML while the second one expects a string (in the body of the HTTP POST request). 第一个请求一个XML,第二个请求一个字符串(在HTTP POST请求的主体中)。
  • The first one sends back some XML data () while the second one sends back a string ('thanks') 第一个发回一些XML数据(),而第二个发回字符串('thanks')

Regarding the 'content-type' setting: application/xml for the first one and plain/text for the second one. 关于“内容类型”设置:第一个是application / xml,第二个是纯文本。

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

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