本文介绍了如何避免< string xmlns ="http://schemas.microsoft.com/2003/10/Serialization/"> ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用VB.NET"WCF Rest Service应用程序"项目模板构建了一个非常简单的Web服务(这是一个好选择吗?).除了存在
I have built a very simple web service using the VB.NET "WCF Rest Service Application" project template (Is this a good choice?). I works well except the fact that there is
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
...
</string>
添加到我的答案中.
我已将我的返回值声明为String:
I have declared my return value as a String :
<WebInvoke(UriTemplate:="member/login", Method:="POST",
responseformat:=WebMessageFormat.Json,
BodyStyle:=WebMessageBodyStyle.Bare)>
Public Function Create(data As IO.Stream) As String
Dim strData As String = New IO.StreamReader(data).ReadToEnd()
Dim UserAccessForm As LoginAccess = Me.getAnswer(strData)
Dim jsonAnswer As String
jsonAnswer = Newtonsoft.Json.JsonConvert.SerializeObject(UserAccessForm, Newtonsoft.Json.Formatting.None)
Return jsonAnswer
End Function
所以不是这样回答:
{"logged":false,"userID":"0","message":"Empty body"}
我得到了:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"logged":false,"userID":"0","message":"Empty body"}
</string>
有什么办法可以避免我的字符串答案的这种不必要的序列化?
推荐答案
我只是遇到了这个问题,并通过将XmlSerializerFormat
属性应用于服务合同来解决了这个问题.
I just ran into this problem and solved it by applying the XmlSerializerFormat
attribute to the service contract.
这是一个C#代码段-希望对您有所帮助.
Here's a c# snippet - hope it helps you.
[ServiceContract(Namespace = "")]
[XmlSerializerFormat]
interface IHuggies
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/CheckIfConsumerExists")]
bool CheckIfConsumerExists(string parameters);
}
这篇关于如何避免< string xmlns ="http://schemas.microsoft.com/2003/10/Serialization/"> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!