Search code examples
asp.net-mvcodatarestier

RESTier OData Function returning EntitySet. "The related entity set could not be found from the OData path"


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);

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()"
      }
    }
  }
}

How do I assign the right return EntitySet Collection on RESTier? I wanted to return the list of objects/ IQueryable


Solution

  • There are two places you need to modify to make it work:

    1) You need to add a collection return type when declaring the action.

    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).