c# - 备用键 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));
解决方案
尝试以下选项之一:
在 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;
})
推荐阅读
- ios - 按需滚动不断调用
- java - FirebaseUI - 不兼容的类型:字符串无法转换为 DatabaseReference
- python - 为什么这段代码会打印 28?
- visual-studio - 为什么 Visual Studio 中的调试和发布版本的代码覆盖率结果不同
- python - 如何在 Python 3 中的列表中向后减去?
- android - 如何使用 MAC 地址 [Android] 获取本地网络中任何设备的动态 ip?
- laravel-blade - Laravel 5.6 错误“函数名必须是字符串”,内置身份验证
- angular - 角度 5 背景图像不绑定某些 android 设备?
- amazon-web-services - AWS-Auto Scaling 上的 AWS cloudwatch 自定义指标
- android - 无法解决:play-services-tasks