OData and .NET Core 2 Web API - disable case-sensitivity?

℡╲_俬逩灬. 提交于 2021-02-08 02:56:16

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/48347919/odata-and-net-core-2-web-api-disable-case-sensitivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!