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

Answers


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.


Need Your Help

Not filling combo box data in all rows of data grid view in windows application

c# vb.net winforms datagridview

I have a data grid view. 3rd row of my data grid view column is combo box, in every row of 3rd column i want to come that combo box with some particular data. So in form load event and data grid v...

Wamp Server Offline error

wamp

Some of you might get the “Server Offline” error after you install WAMP on your windows 7 system. This happens if IIS server was installed in the system prior to the installation of WAMP thus creat...