【问题标题】:Alternate keys oData url is not working with Microsoft.AspNet.oData 7.1.0备用键 oData url 不适用于 Microsoft.AspNet.oData 7.1.0
【发布时间】:2019-05-19 10:55:22
【问题描述】:

我正在尝试为我的 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# odata asp.net-core-webapi


    【解决方案1】:

    尝试以下选项之一:

    • 调整网址中的大小写: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;
    })
    

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 2012-10-09
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 2018-02-11
      相关资源
      最近更新 更多