备用键 oData url 不适用于 Microsoft.AspNet.oData 7.1.0



我正在尝试为我的 Edmmodel 添加备用键,然后能够使用 oData url 查询它。我尝试在启动文件中配置路由生成器,并在模型生成器中添加了备用 ksys。但是,尽管做了所有的事情,当我尝试访问 url https://localhost:44357/odata/books(ISBN='978-0-321-87758-1'(时,我仍然收到 404 未找到(。以下是详细信息

这是我的启动.cs文件

       public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseMvc(routeBuilder =>
            {
                routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
                IEdmModel model = GetEdmModel();
                routeBuilder.MapODataServiceRoute("odataRoute", "odata", containerBuilder =>
                {
                    containerBuilder.AddDefaultODataServices()
                        .AddService<IEdmModel>(Microsoft.OData.ServiceLifetime.Singleton,
                            s => model)
                        .AddService<IEnumerable<IODataRoutingConvention>>(Microsoft.OData.ServiceLifetime.Singleton,
                            serviceProvider => ODataRoutingConventions.CreateDefaultWithAttributeRouting("odataRoute", routeBuilder))
                        .AddService<ODataUriResolver>(Microsoft.OData.ServiceLifetime.Singleton,
                            s => new AlternateKeysODataUriResolver(model));
                });
            });
        }
        private IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Book>("Books");
            builder.EntitySet<Press>("Press");
            var model = builder.GetEdmModel();
            //Add alternate keys
            // first, find entity type
            IEdmEntityType booksEntityType = model.FindDeclaredEntitySet("Books").EntityType();
            // now find the properties we want to use as alternateKey
            var isbnEdmProp = booksEntityType.FindProperty("ISBN");
            // and finally add the annotation
            ((EdmModel)model).AddAlternateKeyAnnotation(booksEntityType, new Dictionary<string, IEdmProperty> {
                {
                    "ISBN", isbnEdmProp
                }
            });
            return model;
        }

图书控制器.cs

        [EnableQuery]
        public IActionResult GetByISBN(string keyISBN)
        {
            return Ok(_db.Books.FirstOrDefault(c => c.ISBN == keyISBN));
        }

我的问题

1. When I try to access using alternate key with the url 
https://localhost:44357/odata/books(ISBN='978-0-321-87758-1') I am 
receiving 404 Not Found response. Can someone tell me what am I missing here ?
2. What I also noticed that with these changes even the basic routes like simple GET https://localhost:44357/odata/books is also not working

更新
我能够隔离出下面的变化正在破坏所有现有的路线。配置备用键的正确方法是什么?有人可以帮助我以下配置有什么问题吗.AddService(Microsoft.OData.ServiceLifetime.Singleton, s => new AlternateKeysODataUriResolver(model((;

尝试以下选项之一:

  • 调整网址中的大小写:https://localhost:44357/odata/Books(ISBN='978-0-321-87758-1'(

  • 在创建 AlternateKeysODataUri 解析程序实例时显式管理区分大小写:

.AddService<ODataUriResolver>(Microsoft.OData.ServiceLifetime.Singleton, s =>
{
    var resolver = new AlternateKeysODataUriResolver(model);
    resolver.EnableCaseInsensitive = true;
    return resolver;
})

最新更新