2010-10-13 51 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

相關問題