如何将裸结果返回给WCF客户端

use*_*687 3 wcf wcf-rest

我有类似以下代码:

<OperationContract()>
<Description("")>
<WebGet(Bodystyle:=WebMessageBodyStyle.Bare, UriTemplate:="TestConnection")>
Function TestConnection() As String


Public Function TestConnection() As String Implements ITestSvc.TestConnection
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"
    Return "Connection Success"
End Function
Run Code Online (Sandbox Code Playgroud)

但它返回的是 <string xmlns='...'>Connection Success</string>

如何在没有XML包装器的情况下返回"连接成功".我知道我们可以用MessageEncoder做些什么.但是,我希望在操作级别提供它(某些操作需要XML/JSON包装器,而某些操作则不需要).

谁可以帮我这个事?

小智 12

这是返回纯文本的最简单的解决方案.将响应格式设置为xml并将outgoingresponse设置为text/html.应该做的伎俩.

[WebGet(ResponseFormat = WebMessageFormat.Xml)]
public string DoWork()
{

    WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
    return "THIS IS PLAIN TEXT";
}
Run Code Online (Sandbox Code Playgroud)