Klaus
Klaus

Reputation: 1

OData function with parameter of type "Collection of Entities or Entity References"

I have a question regarding an OData bound function with parameters of type “Collection of Entity or Entity Reference”. When I tried to call the function I do not get into my function but get HTTP errors (see below). Description of this parameter type is from http://odata.github.io/WebApi/#04-06-function-parameter-support . I am implementing an OData V4 service with ASP.Net MVC 2 and ASP.NET OData 5.7.0, programming language C#.

My code for the function:

[HttpGet]  
public IHttpActionResult GetTimeSeriesTest2([FromODataUri] IEnumerable<Tag> tags)  
{  
     ... some Code  
}   

My code for the function declaration in the model:

var tag = builder.EntityType<Tag>();  
funcCol = tag.Collection.Function("GetTimeSeriesTest2");  
funcCol.ReturnsCollectionFromEntitySet<Measurement>("Measurements");  
funcCol.CollectionEntityParameter<Tag>("tags");  

There is a “Tags” collection of tag entities accessible and a tag has a key of type string. Query to the Tags collection and to get a single tag works fine (e.g. http ://localhost:51100/odata/Tags('0') )

The call for the OData routing in WebApiConfig.cs:

config.MapODataServiceRoute("OData", "odata", model: GetEdmModel());

My OData query is used in Internet Explorer:
With query:
http ://localhost:51100/odata/Tags/Default.GetTimeSeriesTest2(tags=@x)?@x={\"value\":[{\"@odata.id\":\"http://localhost:51100/odata/Tags('0')\"}]}
-> I get an HTTP Error 404.0 – Not Found.

If I make the function unbound and call it without Namespace “Default” then the function is executed but the tags parameter is empty. If I change the IEnumerable type from Tag to Int and use the following query everything works fine: http ://localhost:51100/odata/Tags/Default.GetTimeSeriesTest2(tags=@x)?@x=[4,5,6]

What went wrong and how to solve it?
Any help is welcome and appreciated.

Thanks in advance

Klaus-Peter

Upvotes: 0

Views: 1842

Answers (1)

Lukasz S
Lukasz S

Reputation: 668

You should encode Tag data parameter. The url should be

http ://localhost:51100/odata/Tags/Default.GetTimeSeriesTest2(tags=@x)?@x=%7B%22value%22%3A%5B%7B%22%40odata.id%22%3A%22http%3A%2F%2Flocalhost%3A51100%2Fodata%2FTags('0')%22%7D%5D%7D

You can use encodeURIComponent method in JS to encode.

Upvotes: 1

Related Questions