问题描述
我有很多不同的实体需要启用 OData.这些实体根据其类型分为不同的组.目前,默认设置是将 EntitySet 与控制器名称相匹配,但我不希望每个实体类型都有一个控制器.有没有一种方法可以将多个实体集映射到一个控制器.
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.
我尝试让我感兴趣的类型实现一个通用接口,并将该接口指定为我的实体集类型.我还尝试在一个控制器中包含两个实体并使用它们自己的 get 请求,但没有运气.我还尝试定义自己的路由类来扩展 EntitySetRoutingConvention,但没有让它起作用.
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
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();
}
这将查找名为 MyEntity1Controller 和 MyEntity2Controller 的控制器.我想要的是这样的:
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
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();
}
预期的结果是我可以转到 myurl/Generic/MyEntity1,这会在我的通用控制器中触发 GET 请求.我还应该能够执行 odata 操作,例如 myurl/Generic/MyEntity1?$select=Id.
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.
推荐答案
添加 ODataRoute 属性
Add the ODataRoute Attribute
/// MyController.cs
// GET: odata/myentity1
[EnableQuery]
[ODataRoute("myentity1")]
public IQueryable<MyEntity1> GetMyEntity1() => db.MyEntity1.AsQueryable();
// GET: odata/myentity2
[EnableQuery]
[ODataRoute("myentity1")]
public IQueryable<MyEntity2> GetMyEntity2() => db.MyEntity2.AsQueryable();
这篇关于如何将多个实体集绑定到一个 odata 控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!