无法转换System.Collections.Generic.IEnumerable类型的对象 - Web API问题

时间:2016-09-13 18:54:51

标签: vb.net asp.net-web-api ienumerable

所以我试图在VS2015中用VB创建一个简单的Web API 2。但是,我确实使用的代码只返回一个简单的字符串,其中 Web Api需要序列化数据,因此可以返回XML或JSON。

'Here I initialize and populate my object       
Public widgetObj As New widgetModelClass("bottle", 1)


' Below kind of works, but simply returns a string **instead** of 
' the serialized data for XML or JSON the Web API should return. 
' it returns: {"Name":"bottle","ID":1}
 Public Function GetValues() As widgetModelClass
     Return widgetObj 
 End Function

以下代码失败,例外情况为:

  

无法转换System.Collections.Generic.IEnumerable

类型的对象
Public Function GetValues() As IEnumerable(Of widgetModelClass)
    Return widgetObj
End Function   

我已将模型类定义如下

Public Class widgetModelClass
    Public Sub New(ByVal name As String, ByVal id As Integer)
        Me.Name = name
        Me.ID = id
    End Sub

    Private m_Name As String
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property

    Private m_ID As Integer
    Public Property ID() As Integer
        Get
            Return m_ID
        End Get
        Set
            m_ID = Value
        End Set
    End Property
End Class

完整错误消息

  

错误有   发生了。无法转换类型的对象   输入'TestApp.widgetModelClass'   'System.Collections.Generic.IEnumerable 1[TestApp.widgetModelClass]'.</ExceptionMessage><ExceptionType>System.InvalidCastException</ExceptionType><StackTrace> at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary 2个参数,CancellationToken   的CancellationToken)   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

1 个答案:

答案 0 :(得分:1)

在您的示例中,您返回widgetObj,但它不是您想要的IEnumerable(Of widgetModelClass)的实例。

您必须更改声明以返回widgetModelClass的一个实例:

Public Function GetValues() As widgetModelClass
    Return widgetObj
End Function

或您的return语句返回IEnumerable(Of widgetModelClass)

Public Function GetValues() As IEnumerable(Of widgetModelClass)
    Return New List(Of widgetModelClass)(New widgetModelClass() { widgetObj } )
End Function