首页 > 解决方案 > 备用键 oData url 不适用于 Microsoft.AspNet.oData 7.1.0

问题描述

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

这是我的 Startup.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;
        }

BooksController.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));

标签: c#odataasp.net-core-webapi

解决方案


尝试以下选项之一:

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

  • 在创建 AlternateKeysODataUriResolver 实例时显式管理区分大小写:

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

推荐阅读