为什么Web服务返回null?

时间:2011-07-15 11:15:46

标签: .net vb.net json .net-3.5 asmx

我有以下网络服务:

<%@ WebService Language="VB" Class="WebService" %>

Imports System.Web
Imports System.Web.Script.Services
Imports System.Web.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class WebService
    Inherits System.Web.Services.WebService

    Public Class Person
        Public FirstName As String
        Public LastName As String

        Public Sub New(ByVal m_FirstName As String, ByVal m_LastName As String)
        End Sub
    End Class

    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    <WebMethod()> _
    Public Function GetPersons() As List(Of Person)
        Dim lst As List(Of Person) = New List(Of Person)

        lst.Add(New Person("firstname_1", "surname_1"))
        lst.Add(New Person("firstname_2", "surname_2"))

        Return lst
    End Function
End Class

但它正在回归:

{"d":[{"FirstName":null,"LastName":null},{"FirstName":null,"LastName":null}]}

它应该返回:

{"d":[{"FirstName":"firstname_1","LastName":"surname_1"},{"FirstName":"firstname_2","LastName":"surname_2"}]}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您从不在构造函数中赋予FirstName和LastName,因此它们的值将保持为null。 将以下内容添加到构造函数中:

FirstName = m_FirstName
LastName = m_LastName

答案 1 :(得分:0)

我的VB有点生疏,但我没有在Person类中看到FirstName和LastName属性。我只看到变量。 (以及之前提到的缺失任务)