Is it possible to expose multiple Odata v4 endpoints in Asp.Net WebApi project

时光毁灭记忆、已成空白 提交于 2019-12-01 14:04:20

问题


The title explains the requirement. Is it possible to expose multiple endpoints in single project?

Something like:

  • http://localhost:8000/svc1/$metadata
  • http://localhost:8000/svc2/$metadata
  • http://localhost:8000/svc3/$metadata

Because I need to divide functionality into multiple components. Can anyone help me?

UPDATE

Currently I'm using below code to create and expose Odata service.

public void Configuration(IAppBuilder app)
{
   HttpConfiguration config = new HttpConfiguration();
   ConfigureRoute(config);
   ConfigureGlobalFilters(config);

   HttpServer server = new HttpServer();
   ODataBatchHandler batchHandler = new DefaultODataBatchHandler(server);
   config.MapODataServiceRoute("Odata", "Odata", GenerateEdmModel(), batchHandler);


   ...

   config.EnsureInitialized();
}

private IEdmModel GenerateEdmModel()
{
   ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
   builder.Namespace = "ServiceA";
   builder.ContainerName = "DefaultContainer";

   builder.EntitySet<Permission>("ApplicationPermissions");

   return builder.GetEdmModel();
}

I would like to expose separate services for each component (under different namespaces?).


回答1:


The following line should be the one you care about:

config.MapODataServiceRoute("Odata", "Odata", GenerateEdmModel(), batchHandler);

The second string parameter is the routePrefix, which means currently you're probably hitting http://yourhost.com/Odata/$metadata. If you simply create another mapping with a different prefix value (e.g. Odata2) you'd be able to make calls against http://yourhost.com/Odata/$metadata AND http://yourhost.com/Odata2/$metadata. You'll probably want to give them both a unique routeName as well though (the first string parameter), and you'll probably want to provide a different model as well so the two services will actually be different :).



来源:https://stackoverflow.com/questions/43966261/is-it-possible-to-expose-multiple-odata-v4-endpoints-in-asp-net-webapi-project

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