Search code examples
c#odataversioningcase-sensitive

microsoft/aspnet-api-versioning - case-insensitive URI endpoints


I have created OData Web Service using Microsoft.AspNet.OData(7.2.0) with .NET Framework 4.7.

My OData Web Service was working correctly, the URI endpoints were case-insensitive, like:

  • http://${host}/Value(256)/ was working correctly (using ValueController)
  • http://${host}/value(256)/ was working correctly (using the same ValueController)

Later, I enhanced my OData Web Service with versioning using Microsoft.AspNet.OData.Versioning(4.0.0)

My OData Web Service works correctly only for the case-sensitive URI endpoints now, like:

  • http://${host}/Value(256)?api-version=1.0 is working correctly (using ValueController)
  • http://${host}/value(256)?api-version=1.0 is not working anymore and returns 404 - File or directory not found.

Please how to set up the URI Endpoints to be case-insensitive after the Microsoft.AspNet.OData.Versioning(4.0.0) came into the project?


Solution

  • I can't tell you the exact reason why adding versioning via Microsoft.AspNet.OData.Versioning fails, but I can provide a workaround. See the CoinfigureContainer method below.

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.AddApiVersioning(setup =>
            {
                setup.DefaultApiVersion = new Microsoft.Web.Http.ApiVersion(1, 0);
                setup.AssumeDefaultVersionWhenUnspecified = true;
            });
    
            VersionedODataModelBuilder modelBuilder = new VersionedODataModelBuilder(config)
            {
                ModelBuilderFactory = () => new ODataConventionModelBuilder().EnableLowerCamelCase(),
                ModelConfigurations = { new xxxxConfiguration(), new yyyyConfiguration() }
            };
    
            config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
    
            config.MapVersionedODataRoutes("ODataRoute", "odata/v{apiVersion}", modelBuilder.GetEdmModels(), ConfigureContainer);
    
            config.MapHttpAttributeRoutes();
        }
    
        static void ConfigureContainer(IContainerBuilder builder)
        {
            builder.AddService<ODataUriResolver>(ServiceLifetime.Singleton, sp => new UnqualifiedCallAndEnumPrefixFreeResolver() { EnableCaseInsensitive = true });
        }
    }
    

    My speculation is that the addition of the versioning library somehow replaces the ODataUriResolver that has the EnablesCaseInsensitive = true property set. This code "resets" the ODataUriResolver.

    FYI - I stumbled across the code that this is based on at https://github.com/microsoft/aspnet-api-versioning/blob/master/samples/webapi/BasicODataWebApiSample/Startup.cs.