Additional GetXYZ methods in EntitySetController

别说谁变了你拦得住时间么 提交于 2019-12-13 17:18:52

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/17261450/additional-getxyz-methods-in-entitysetcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!