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

时间:2019-04-15 06:21:30

标签: c# odata asp.net-core-webapi

我正在尝试为我的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 =>新的AlternateKeysODataUriResolver(model));

1 个答案:

答案 0 :(得分:0)

尝试以下选项之一:

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

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

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