Search code examples
c#asp.netasp.net-web-apiodataodata-v4

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


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

Something like:

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?).


Solution

  • 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 :).