Whitney Kew
Whitney Kew

Reputation: 225

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

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

Upvotes: 2

Views: 1656

Answers (1)

Victor Wilson
Victor Wilson

Reputation: 1846

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.

Upvotes: 3

Related Questions