RESTier OData函数返回EntitySet. “无法从OData路径中找到相关的实体集". [英] RESTier OData Function returning EntitySet. "The related entity set could not be found from the OData path"
问题描述
在RESTier OData上添加了功能
Added a function on RESTier OData
var ambilLocationsByMarketId = new EdmAction(ns, "AmbilLocationsByMarketId", location.GetEdmTypeReference(true), false, null);
model.AddElement(ambilLocationsByMarketId);
var entityContainer = (EdmEntityContainer)model.EntityContainer;
entityContainer.AddActionImport("AmbilLocationsByMarketId", ambilLocationsByMarketId);
在我的DomainController中实现了自定义功能
Implemented the custom function in my DomainController
[HttpGet]
[EnableQuery]
[ODataRoute("AmbilLocationsByMarketId")]
public IHttpActionResult AmbilLocationsByMarketId()
{
var locations = DbContext.Locations.Where(l => l.Name.Contains("Hotel")).Select(l => l);
return Ok(locations);
}
但是我一直得到这个结果作为回报
But I keep getting this result in return
{
"error": {
"code": "",
"message": "An error has occurred.",
"innererror": {
"message": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.",
"type": "System.InvalidOperationException",
"stacktrace": "",
"internalexception": {
"message": "The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.",
"type": "System.Runtime.Serialization.SerializationException",
"stacktrace": " at System.Web.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\\\r\\\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\\\r\\\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\\\r\\\n--- End of stack trace from previous location where exception was thrown ---\\\r\\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\\r\\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\\r\\\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\\\r\\\n at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
}
}
}
}
如何在RESTier上分配正确的返回EntitySet集合?我想返回对象列表/IQueryable
推荐答案
您需要修改两个地方才能使其正常工作:
There are two places you need to modify to make it work:
1)声明操作时,您需要添加 collection 返回类型.
1) You need to add a collection return type when declaring the action.
2)添加ActionImport时,需要添加EdmEntitySetReferenceExpression.
2) You need to add an EdmEntitySetReferenceExpression when adding the ActionImport.
Please see https://github.com/OData/RESTier/blob/master/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Domain/TrippinDomain.cs#L177-L189 for an example (though it is a FunctionImport but should apply to an ActionImport too).
这篇关于RESTier OData函数返回EntitySet. “无法从OData路径中找到相关的实体集".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!