Search code examples
asp.net-web-apiodata

Additional GetXYZ methods in EntitySetController


I want to be able to have additional GetXYZ methods in my EntitySetController derived controller class. For example:

[HttpGet]
 [Queryable]
        public string GetAirportsWithinRadius(int airportId, int radius)
        {

            var resultAirports = GetAirportsWithinRadius2(airportId, radius);

            return resultAirports;            
        }

This is what I have for config:

ActionConfiguration getAirportsWithinRadius =  modelBuilder.Entity<Airport>().Collection.Action("GetAirportsWithinRadius");
        getAirportsWithinRadius.Parameter<int>("airportId");
        getAirportsWithinRadius.Parameter<int>("radius");
        getAirportsWithinRadius.ReturnsCollectionFromEntitySet<Airport>("Airports");

I want this action to be composable just like the default Get Queryable action, but this would be an alternative that supports all odata parameters but additionally an airportId and radius. This would first filter airports by a radius search (this I know how to do - it's irrelevant to the question) and then return the Queryable so that it can be further filtered by odata params.

Everything I read says this would be an odata action and therefore must be a POST, but Get is also an action and that is a GET, so why not allow extended getters with additional parameters? Am I missing something? How do I accomplish what I want to get done?

I would call this from an ajax client as such: GET /odata/Airports?$inlinecount=allpages&$top=25&airportId=2112&radius=50

as opposed to a regular odata GET: GET /odata/Airports?$inlinecount=allpages&$top=25

Thanks

EDIT: I understand now that this is an odata "function" and it is under consideration as a future feature. Let's forget for second the odata meaning of this. It is essentially a WebApi HttpGet that returns a Queryable, right? So, as long as I don't care about the metadata advertising of this "function", how can I make sure that it is a reachable HttpGet form a route perspective inside of an ODataController? The ODataController needs the MapODataRoute and can I additionally add non odata routes using additional MapHttpRoutes? I ask this because it seems to me that I should be able to, but all my tries have failed (trying to hit the HttpGet through fiddler). I can find no examples on extending an ODataController with additional non-odata GETs. Can someone help me understand if and how this can be done with the example?:

[Queryable]
        public IQueryable<Airport> Get()
        {
            return db.Airports;
        }
    [HttpGet]
     [Queryable]
            public string GetAirportsWithinRadius(int airportId, int radius)
            {

                var resultAirports = GetAirportsWithinRadius2(airportId, radius);

                return resultAirports;            
            }

Solution

  • You are looking for OData Functions, which is not yet supported out of the box. We have an issue over here. You can up-vote it.

    http://aspnetwebstack.codeplex.com/workitem/881