How do I bind multiple entity sets to one odata controller?

允我心安 提交于 2019-12-11 06:09:13

问题


I have a lot of different entities that I want to enable OData for. These entities are categorized into different groups based on their type. Currently, the default is to match the EntitySet with the controller name, but I don't want a controller for every entity type that I'll have. Is there a way I can map multiple EntitySets to one controller.

I've tried having the types I'm interested in implement a common interface and specified that interface as my entity set type. I also tried having two entities within one controller and with their own get requests, but no luck. I also tried defining my own routing class that extends EntitySetRoutingConvention, but haven't gotten that to work.

WebApiConfig

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet<MyEntity1>("MyEntity1");
            builder.EntitySet<MyEntity2>("MyEntity2");
            config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
            // Web API routes
            config.MapHttpAttributeRoutes();
        }

This looks for controllers named MyEntity1Controller and MyEntity2Controller. What I want is something like:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet<MyEntity1>("Generic");
            builder.EntitySet<MyEntity2>("Generic"); // Throws an error since Generic is already registered to MyEntity1
            config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
            // Web API routes
            config.MapHttpAttributeRoutes();
        }

GenericController.cs

// GET: odata/myentity1
        [EnableQuery]
        public IQueryable<MyEntity1> GetMyEntity1()
        {
            return db.MyEntity1.AsQueryable();
        }

        // GET: odata/myentity2
        [EnableQuery]
        public IQueryable<myentity2> GetMyEntity2()
        {
            return db.MyEntity2.AsQueryable();
        }

The expected results would be I can go to myurl/Generic/MyEntity1 and that would hit a GET request in my Generic Controller. I should also be able to perform odata operations such as myurl/Generic/MyEntity1?$select=Id.

来源:https://stackoverflow.com/questions/57616954/how-do-i-bind-multiple-entity-sets-to-one-odata-controller

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