使用ASPNET Core 3.1端点路由的OData备用键配置

时间:2020-05-19 12:12:55

标签: asp.net-core odata

我正在尝试配置ODATA备用密钥,同时使用ODATA V7,Aspnet Core 3.1和端点路由。看来我错过了某个地方的配置步骤。

我认为我需要配置一个AlternateKeysODataUriResolver,但是对于端点路由,我没有找到需要这样做的地方。

我的代码如下:

...
<div class="col">
         <product-card [product] = "p" [shopping-cart] = "cart"></product-card>
      </div>
...

然后在我的odata控制器中

SELECT
orders_id,
orders_location
FROM orders
WHERE orders_id IN (1234, 1235, 1239, 1242)

2 个答案:

答案 0 :(得分:0)

@Scott

希望以下代码可以为您提供帮助吗?

    app.UseEndpoints(endpoints =>
    {
            ...
            endpoints.MapODataRoute("odata", "odata",
                    builder =>
                        builder.AddService(ServiceLifetime.Singleton, sp => model)
                            .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp =>
                                ODataRoutingConventions.CreateDefaultWithAttributeRouting("odata", configuration))
                            .AddService<ODataUriResolver>(ServiceLifetime.Singleton, sp => new AlternateKeysODataUriResolver(model)));
   ...
}

答案 1 :(得分:0)

Sam Xu的答案朝着正确的方向发展,您必须像这样映射OData路由,但由于您没有HttpConfiguration(配置变量),因此无法正常工作。

对于端点路由,请使用endpoints.ServiceProvider

我在这里为您编写了一个示例:

var model = GetEdmModel();
app.UseEndpoints(endpoints =>
{
    ...
    endpoints.MapODataRoute("odata", "odata", builder => builder
        .AddDefaultODataServices()
        .AddService(Microsoft.OData.ServiceLifetime.Singleton, s => model)
        .AddService<IEnumerable<IODataRoutingConvention>>(Microsoft.OData.ServiceLifetime.Singleton, sp => ODataRoutingConventions.CreateDefaultWithAttributeRouting("odata", endpoints.ServiceProvider))
        .AddService<ODataUriResolver>(Microsoft.OData.ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model) { EnableCaseInsensitive = true })
    );
});

请注意,我还在ODataUriResolver中设置了EnableCaseInsensitive = true,以使用不区分大小写的路由前缀。