【问题标题】:Returning empty string from Web Service从 Web 服务返回空字符串
【发布时间】:2014-08-24 23:22:09
【问题描述】:

我创建的 Web 服务存在问题:我的 Web 服务正在返回 Stream (我不希望函数返回字符串..只是流!)问题是如果流为空,它开始下载一个空的文件。 如何强制代码返回纯空 JSON 字符串而不是空文件。 (如果假设我没有将一个参数传递给它开始下载一个空文件)

如果流是空的,我想像这样返回空字符串: {"results" : [],"status" : "ZERO_RESULTS"} 到函数!

这是我的网络服务:

Public Class Service1
    Dim m_SelPerson As String = String.Empty
    <OperationContract()>
    <WebGet(ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/getPersonInfo/?personID={personID}&companyCode={companyCode}", BodyStyle:=WebMessageBodyStyle.Bare)>
    Public Function getPersonInfo(ByVal personID As String, ByVal companyCode As String) As Stream
        Try
            Dim dba As New DBAccess
            Dim person As New PersonInfo
            Dim ds As DataSet = dba.GetPersonInfo(personID, companyCode)
            If Not ds Is Nothing Then
                Dim dr As DataRow = ds.Tables(0).Rows(0)
                person = New PersonInfo
                If Not String.IsNullOrEmpty(dr("UserID")) Then
                    person.UserID = Convert.ToInt32(dr("UserID"))
                End If
                person.PersonID = Convert.ToInt32(dr("PersonID"))
                person.Company = dr("Company")
                person.Title = dr("Title")
                person.CellNum = dr("CellNum")
                person.EmergencyPhone = dr("EmergencyPhone")
                person.Email = dr("Email")
                person.PersonImageName = dr("PersonImageName")
                Dim oSerilzer As New System.Web.Script.Serialization.JavaScriptSerializer
                m_SelPerson = oSerilzer.Serialize(person)
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"
                Return New MemoryStream(Encoding.UTF8.GetBytes(m_SelPerson))
                'Return m_SelPerson
            Else
                Return New MemoryStream(m_SelPerson)
            End If
            'Return New MemoryStream(Encoding.UTF8.GetBytes(m_SelPerson))
        Catch ex As Exception
            Return New MemoryStream(m_SelPerson)
        End Try
    End Function
End Class

【问题讨论】:

  • 你有这个标记为 c# 但这似乎是 VB.NET,为什么 c# 标记?

标签: asp.net json vb.net web-services wcf


【解决方案1】:

不仅仅是设置 m_SelPerson="{""results"" : [],""status"" : ""ZERO_RESULTS""}"

为什么不把它做成一个漂亮的 xml 字符串...

获取数据集,无需解析任何内容... 使用 XML 从表数据中创建一个包含列和值的 XMDocument。 DS 是你的数据集..

 Dim XMLDOX As New System.Xml.XmlDocument

 Try
     XMLDOX.LoadXml(DS.GetXml)
 Catch
     ' deal with it.
 End try

  Dim ms As Io.MemoryStream
  ms = New MemoryStream(New ASCIIEncoding().GetBytes(XMLDOX.InnerXml.ToString))

那么你的内存流是一个很好的 XML 表,它包含列和值,由它所用的数据集表定义,并且非常易于使用。

 <table1><row><UserID>something</userid><PersonID>123</PersonID></Company>... and so on...  </row></table>

您可以先测试数据集的行,然后编码您的应用在其为空时可以使用的响应。

  if ds.tables(0).rows.count=0 then 
       ms = New MemoryStream(New ASCIIEncoding().GetBytes("</noresults>"))
   end if 

【讨论】:

  • OP 正在返回 JSON,而不是 XML。 &lt;WebGet(ResponseFormat:=WebMessageFormat.Json,
猜你喜欢
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多