I am looking to create an unbound function inside ProductController which return entirely different entity(Not related to Product).
[EnableQuery]
public class ProductsController : ODataController
{
[HttpGet]
[ODataRoute("InvokeMyUnBoundFunction(Id={id})")]
public IHttpActionResult InvokeMyUnBoundFunction(int id)
{
TestUnBound testObj= new TestUnBound();
testObj.Name = "Test" + id;
return Ok(testObj);
}
}
and my webApiConfig is
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
builder.EntitySet<TestUnBound>("TestUnBounds"); //Its not related to Product.
builder.Function("InvokeMyUnBoundFunction").Returns<TestUnBound>().Parameter<int>("Id");
But when I invoked http://localhost:port/api/odata/InvokeMyUnBoundFunction(Id=1234) I got an error message like
"The related entity set or singleton cannot be found from the OData path. The related entity set or singleton is required to serialize the payload."
Am I missed any concepts?
You should use
ReturnsFromEntitySet<TestUnBound>
instread of
Returns<TestUnBound>
In OData v4, change the function declaration to:
builder.Function("InvokeMyUnBoundFunction").ReturnsCollectionFromEntitySet<TestUnBound>().Parameter<int>("Id");
instead of ReturnsFromEntitySet<TestUnBound>
as suggested by answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With