简体   繁体   English

是否可以为ASPNET.Core OData声明多个路由

[英]Is it possible to declare multiple routes for ASPNET.Core OData

I'm using the Microsoft.AspNetCore.OData 7.0.0 nuget package. 我正在使用Microsoft.AspNetCore.OData 7.0.0 nuget包。

I've registered my OData route prefix like the following. 我已经注册了我的OData路由前缀,如下所示。

app.UseMvc(routeBuilder =>
{
    var builder = new ModelODataBuilder();
    routeBuilder.EnableDependencyInjection();
    routeBuilder.MapODataServiceRoute("ODataRoutes", "odata/Route1", builder.GetEdmModel(app.ApplicationServices));
});

Each OData Controller has a ODataRoutePrefix where the prefix corresponds to an EntitySet declared and configured in the GetEdmModel. 每个OData控制器都有一个ODataRoutePrefix,其前缀对应于在GetEdmModel中声明和配置的EntitySet。 For example 例如

[ODataRoutePrefix("Service1")]

However, I need some services to have a route prefix of "odata/Route1" while others have "odata/Route2". 但是,我需要某些服务的路由前缀为“ odata / Route1”,而其他服务的路由前缀为“ odata / Route2”。 For example 例如

http://odata/Route1/Service1 http://odata/Route2/Service2 http:// odata / Route1 / Service1 http:// odata / Route2 / Service2

I can't work out how to configure this. 我不知道如何配置它。 The following code does not work 以下代码不起作用

app.UseMvc(routeBuilder =>
{
    var builder = new ModelODataBuilder();
    routeBuilder.EnableDependencyInjection();
    routeBuilder.MapODataServiceRoute("ODataRoute1", "odata/Route1", builder.GetEdmModelForRoute1(app.ApplicationServices));

    routeBuilder.MapODataServiceRoute("ODataRoute2", "odata/Route1", builder.GetEdmModelForRoute2(app.ApplicationServices));
});

Does anyone know how I would configure this? 有人知道我将如何配置它吗?

I think I found a way. 我想我找到了办法。

added an extra app.UseMvc in my Startup.cs 在我的Startup.cs中添加了一个额外的app.UseMvc

app.UseMvc( b =>
        {
            b.Select( ).Expand( ).Filter( ).OrderBy( ).MaxTop( 100 ).Count( );
            b.MapODataServiceRoute( "odata" , "odata" , EdmModelBuilder.GetEdmModel( app.ApplicationServices ) );
        } );
        app.UseMvc( b =>
        {
            b.Select( ).Expand( ).Filter( ).OrderBy( ).MaxTop( 100 ).Count( );
            b.MapODataServiceRoute( "membership" , "membershipapi" , EdmModelBuilderMembership.GetEdmModel( app.ApplicationServices ) );
        } );

It has been working so far... 到目前为止,它一直在工作...

You would also have to change your code so that you are not using the same path in both mappings: "odata/Route1" 您还必须更改代码,以免在两个映射中使用相同的路径:“ odata / Route1”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于集成测试的ASPNet.Core HostingEnvironment? - ASPNet.Core HostingEnvironment for Integration Tests? 如何将.NetFramework 中的MessageHandler 转换为AspNet.Core - How to convert MessageHandler in .NetFramework to AspNet.Core 如何在 AspNet.Core 中读取 EXIF 数据 - How can i read EXIF data in AspNet.Core Aspnet.core 程序登录但考虑同一个表中的数据 - The Aspnet.core program logs in but considers the data in the same table 在As.net.Core 中,我们可以更改端口号吗? - In Aspnet.Core, can we change port number? ASPNet.Core 从引用的程序集中读取 appsettings? - ASPNet.Core reading appsettings from referenced assemblies? 如何在aspnet.core web api中验证JWT令牌? - How to validate JWT Token in aspnet.core web api? aspnet.core 使用 UserManager 播种数据<identityuser></identityuser> - aspnet.core Seeding data using UserManager<IdentityUser> 如何在ASPNET.Core Web应用程序中使用CORS标头发送HTTP 4xx-5xx响应? - How to send an HTTP 4xx-5xx response with CORS headers in an ASPNET.Core web app? 如果在迭代IEnumerable(从控制器返回)时发生异常,则AspNet.Core返回200 OK且无效的Json - AspNet.Core returns 200 OK and invalid Json if there is an exception while iterating an IEnumerable (returned from controller)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM