是否可以在单个Web API服务中托管两个不同的OData模型? 如果是这样,我怎么能实现这个目标呢?
例: 两个模型,如
var model1 = new ODataConventionModelBuilder();
builder.EntitySet<Car>("Car");
var model2 = new ODataConventionModelBuilder();
builder.EntitySet<Animal>("Animal");
希望通过某种路由可以实现。
Car
和Animal
public class AnimalController:ODataController{
//Implementation stuff
}
public class AnimalController:ODataController{
//Implementation stuff
}
和ModelBuilder
public class ModelBuilder{
public static IEdmModel GetModal1(){
var model1 = new ODataConventionModelBuilder();
builder.EntitySet<Car>("Car");
return model1.GetEdmModel();
}
public static IEdmModel GetModal2(){
var model2 = new ODataConventionModelBuilder();
builder.EntitySet<Animal>("Animal");
return model2.GetEdmModel();
}
}
Modal1的路由
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapODataRoute(
routeName: "OData",
routePrefix: null,
model: ModelBuilder.GetModal1());
}
}
再次提出问题是如何添加modal2
答案 0 :(得分:4)
你可以这样做:
1 - 将两个实体集添加到一个Edm
模型中。
public class ModelBuilder
{
public static IEdmModel GetModal()
{
ODataConventionModelBuilder builder= new ODataConventionModelBuilder();
builder.EntitySet<Car>("Car");
builder.EntitySet<Animal>("Animal");
return model.GetEdmModel();
}
}
2-为不同的Edm
模型添加两个端点。
public static void Register(HttpConfiguration config)
{
config.Routes.MapODataRoute(
routeName: "OData1",
routePrefix: "odata1",
model: ModelBuilder.GetModal1());
config.Routes.MapODataRoute(
routeName: "OData2",
routePrefix: "odata2",
model: ModelBuilder.GetModal2());
}
感谢。
答案 1 :(得分:0)
你应该为汽车创建一个ApiController
,为动物创建一个单独的一个。