在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中实现自定义函数
[HttpGet]
[EnableQuery]
[ODataRoute("AmbilLocationsByMarketId")]
public IHttpActionResult AmbilLocationsByMarketId()
{
var locations = DbContext.Locations.Where(l => l.Name.Contains("Hotel")).Select(l => l);
return Ok(locations);
} 但我总是得到这样的结果
{
"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的列表
发布于 2015-08-03 05:12:24
要使其工作,需要修改两个位置:
1)声明操作时需要添加集合返回类型。
2)添加EdmEntitySetReferenceExpression时需要添加ActionImport。
请参阅https://github.com/OData/RESTier/blob/master/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Domain/TrippinDomain.cs#L177-L189作为一个示例(虽然它是一个FunctionImport,但也应该适用于ActionImport )。
https://stackoverflow.com/questions/31758820
复制相似问题