是否可以在Asp.Net WebApi项目中公开多个Odata v4端点 [英] Is it possible to expose multiple Odata v4 endpoints in Asp.Net WebApi project

查看:91
本文介绍了是否可以在Asp.Net WebApi项目中公开多个Odata v4端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了要求. 是否可以在单个项目中公开多个端点?

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

类似的东西:

  • 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?

更新

当前,我正在使用下面的代码来创建和公开Odata服务.

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

推荐答案

以下一行应该是您关心的:

The following line should be the one you care about:

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

第二个字符串参数是routePrefix,这意味着当前您可能正在点击 http://yourhost. com/Odata/ $ metadata.如果您仅创建另一个具有不同前缀值的映射(例如Odata2),则可以针对 http://yourhost.com/Odata/ $ metadata和 http://yourhost.com/Odata2/ $ metadata.您可能也想同时给它们提供一个唯一的routeName(第一个字符串参数),并且您可能也想提供一个不同的模型,因此这两个服务实际上是不同的:).

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

这篇关于是否可以在Asp.Net WebApi项目中公开多个Odata v4端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆