我收到错误“参数'wheels'属于Edm类型'Collection'。您不能对不是Edm类型'Collection'的参数调用CreateCollectionWriter。”
以下是我的设置的详细信息:
Web API 2.2 OData v4 服务:我在我的服务中的 WheelsController 类中定义了 Action,如下所示:
public async Task<IHttpActionResult> UpdateWheels(ODataActionParameters parameters)
{
object value;
parameters.TryGetValue("carId", out value);
int carId= (int)value;
parameters.TryGetValue("wheels", out value)
IEnumerable<Wheel> wheels = (IEnumerable<Wheel>)value;
// logic goes here....
return OK();
}
在 WebApiConfig.cs 文件中,Action 配置定义如下:
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Car>("Cars");
builder.EntitySet<Wheel>("Wheels");
var action = builder.EntityType<Wheel>().Collection.Action("UpdateWheels");
action.Parameter<int>("carId");
action.CollectionParameter<Wheel>("wheels");
我成功地从 FireFox 浏览器中的 RESTClient 扩展调用上述操作作为对 URL“ http://localhost/Service/Wheels/UpdateWheels ”的 POST 请求,请求正文为
{"carId":2,
"wheels":[{"Id":1,"Name":"Wheel Front 1","Description":"Front wheel left", "PositionEnum":"FrontLeft"},
{"Id":2,"Name":"Wheel Front 2","Description":"Front wheel right", "PositionEnum":"FrontRight"}]
}
但是,当我尝试在客户端应用程序中使用 Simple.OData.Client 调用上述服务操作时,它会出错,例如
public async void TestUpdateWheels(List<Wheel> wheelList)
{
// client is derived from ODataClient from assembly Simple.OData.Client.Core.dll, v4.3.0.0
await client.For<Wheel>()
.Action("UpdateWheels")
.Set(new { carId = 2, wheels = wheelList})
.ExecuteAsync();
}
错误消息:参数“wheels”是 Edm 类型的“Collection”。您不能对不属于 Edm 类型“Collection”的参数调用 CreateCollectionWriter。
如何从 ODataClient 成功调用上述操作?