我是OData的新手,我正尝试使用Microsoft.AspNetCore.OData 7.0.0-beta1 NuGet包将其集成到我们的.NET Core 2.0 Web API中。我希望我的OData URL不区分大小写(即http://localhost:1234/odata/products与http://localhost:1234/odata/Products相同)。我怎么能做到这一点?我的启动代码的相关部分如下:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
// ...
var odataBuilder = new ODataConventionModelBuilder(app.ApplicationServices);
odataBuilder.EntitySet<Product>("products");
app.UseMvc(routeBuilder =>
{
routeBuilder.MapODataServiceRoute("ODataRoute", "odata", odataBuilder.GetEdmModel());
// Workaround for https://github.com/OData/WebApi/issues/1175.
routeBuilder.EnableDependencyInjection();
});
// ...
}
答案 0 :(得分:2)
我自己就是这么想的。您可以参考https://github.com/OData/WebApi/issues/812。
你需要先在项目中添加这样的类:
public class CaseInsensitiveResolver : ODataUriResolver
{
private bool _enableCaseInsensitive;
public override bool EnableCaseInsensitive
{
get => true;
set => _enableCaseInsensitive = value;
}
}
然后你必须以稍微不同的方式创建你的服务路线:
routeBuilder.MapODataServiceRoute("ODataRoute", "odata",
b => b.AddService(ServiceLifetime.Singleton, sp => odataBuilder.GetEdmModel())
.AddService<ODataUriResolver>(ServiceLifetime.Singleton, sp => new CaseInsensitiveResolver()));
这解决了我星期一的情况。