3

我有如下代码:

<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 包装器,而某些操作不需要)。

谁可以帮我这个事?

4

3 回答 3

12

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

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

    WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
    return "THIS IS PLAIN TEXT";
}
于 2012-10-15T14:51:28.660 回答
3

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

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

[OperationContract]
[WebGet(UriTemplate = "foo")]
void Foo()
{
   HttpContext.Current.Response.Write("bar");
}
于 2014-07-16T16:31:08.170 回答
2

答案在这里WCF ResponseFormat For WebGet(它对我有用)

于 2010-10-14T14:37:17.287 回答