简体   繁体   中英

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"

[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. The second one will return

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

With the content type application/xml. And if you want POST a string to it, you will have to send it a XML serialized string. Goofy isn't it.

If you really want to return just a string, then use Stream as your return type. Or take a look at WCF in .Net 4. It looks like they made it a whole lot easier to return other types.

Both methods respond to a POST request on a URI of the format '{BASE_URI}/DoSomething' (just a guess)

  • The first one expects some XML while the second one expects a string (in the body of the HTTP POST request).
  • The first one sends back some XML data () while the second one sends back a string ('thanks')

Regarding the 'content-type' setting: application/xml for the first one and plain/text for the second one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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