无法转换 System.Collections.Generic.IEnumerable 类型的对象 - Web API 问题
Unable to cast object of type System.Collections.Generic.IEnumerable - Web API issue
所以我正在尝试在 VS2015 中使用 VB 创建一个简单的网络 API 2。然而,我确实开始工作的代码 只有 returns 一个简单的字符串 ,其中 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
下面的代码 失败 除了:
Unable to cast object of type System.Collections.Generic.IEnumerable
Public Function GetValues() As IEnumerable(Of widgetModelClass)
Return widgetObj
End Function
我定义了我的模型class如下
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
完整的错误信息:
An error has
occurred.Unable to cast object of type
'TestApp.widgetModelClass' to type
'System.Collections.Generic.IEnumerable1[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 arguments, CancellationToken
cancellationToken)
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at
System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
在你的样本中你 return widgetObj
但它不是你想要的 IEnumerable(Of widgetModelClass)
的实例。
您必须将声明更改为 return widgetModelClass
的一个实例:
Public Function GetValues() As widgetModelClass
Return widgetObj
End Function
或您的 return 声明 return IEnumerable(Of widgetModelClass)
:
Public Function GetValues() As IEnumerable(Of widgetModelClass)
Return New List(Of widgetModelClass)(New widgetModelClass() { widgetObj } )
End Function
所以我正在尝试在 VS2015 中使用 VB 创建一个简单的网络 API 2。然而,我确实开始工作的代码 只有 returns 一个简单的字符串 ,其中 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
下面的代码 失败 除了:
Unable to cast object of type System.Collections.Generic.IEnumerable
Public Function GetValues() As IEnumerable(Of widgetModelClass)
Return widgetObj
End Function
我定义了我的模型class如下
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
完整的错误信息:
An error has occurred.Unable to cast object of type 'TestApp.widgetModelClass' to type '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 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
在你的样本中你 return widgetObj
但它不是你想要的 IEnumerable(Of widgetModelClass)
的实例。
您必须将声明更改为 return widgetModelClass
的一个实例:
Public Function GetValues() As widgetModelClass
Return widgetObj
End Function
或您的 return 声明 return IEnumerable(Of widgetModelClass)
:
Public Function GetValues() As IEnumerable(Of widgetModelClass)
Return New List(Of widgetModelClass)(New widgetModelClass() { widgetObj } )
End Function