2010-10-13 50 views
1

我有代码类似如下:我怎样才能返回裸露的结果返回给WCF客户端

<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 

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

怎样才可以有唯一的“连接成功”,而无需返回XML包装。我知道我们可以用MessageEncoder做些什么。但是,我想让它在操作级别可用(某些操作需要XML/JSON包装,某些操作不需要)。

任何人都可以帮助我吗?

回答

10

这里是返回纯文本的简单的解决方案。将响应格式设置为xml,并将发送响应设置为text/html。应该做的伎俩。

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

    WebOperationContext.Current.OutgoingResponse.ContentType = "text/html"; 
    return "THIS IS PLAIN TEXT"; 
} 
2

有一种方法可以实现这一点,如果你处理HTTP,它不是很好,但我想我可以提到它。

您可以将方法的返回类型设置为void,并将您的原始字符串直接输出到响应中。

[OperationContract] 
[WebGet(UriTemplate = "foo")] 
void Foo() 
{ 
    HttpContext.Current.Response.Write("bar"); 
} 
+0

这很好,它帮助了我,而其他人不,谢谢 – smoothumut 2015-02-14 13:50:53

相关问题