【发布时间】: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