OData和.NET Core 2 Web API-禁用区分大小写吗? [英] OData and .NET Core 2 Web API - disable case-sensitivity?

查看:121
本文介绍了OData和.NET Core 2 Web API-禁用区分大小写吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OData的新手,我正尝试使用 http://localhost:1234/odata/products http://localhost:1234/odata/Products 相同).我该怎么做?我的启动代码的相关部分如下:

I'm new to OData, and I'm trying to integrate it into our .NET Core 2.0 Web API using the Microsoft.AspNetCore.OData 7.0.0-beta1 NuGet package. I would like my OData URLs to be case-insensitive (i.e., http://localhost:1234/odata/products would be the same as http://localhost:1234/odata/Products). How can I accomplish this? The relevant portion of my Startup code is as follows:

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();
    });
    // ...
}

推荐答案

我自己才知道这一点.您可以引用 https://github.com/OData/WebApi/issues/812

I just figured this out myself. You can reference https://github.com/OData/WebApi/issues/812.

总而言之,您首先需要向您的项目添加这样的类:

The long and short of it is that you need to first add a class like this to your project:

public class CaseInsensitiveResolver : ODataUriResolver
{
    private bool _enableCaseInsensitive;

    public override bool EnableCaseInsensitive
    {
        get => true;
        set => _enableCaseInsensitive = value;
    }
}

然后,您必须以稍微不同的方式创建服务路由:

And then you must create your service route in a slightly different manner:

routeBuilder.MapODataServiceRoute("ODataRoute", "odata", 
   b => b.AddService(ServiceLifetime.Singleton, sp => odataBuilder.GetEdmModel())                        
         .AddService<ODataUriResolver>(ServiceLifetime.Singleton, sp => new CaseInsensitiveResolver()));

这解决了我星期一的情况.

This fixed my case of the mondays.

这篇关于OData和.NET Core 2 Web API-禁用区分大小写吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆