16

After upgrading the ASP NET Web API project framework to the Core 2.2 version, the OData route configuration fails. It throws "Cannot use 'Microsoft.AspNet.OData.Routing.ODataRoute' with Endpoint Routing." Exception.

The link https://github.com/Microsoft/aspnet-api-versioning/issues/361 shows how to avoid the exception but disabling the new Core 2.2 routing model. Can you tell me how to solve the problem without deactivating this functionality?

 public IServiceProvider ConfigureServices(IServiceCollection services)
 {
      ...

      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddControllersAsServices();

      ...
 }

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
 {

    ...

    app.UseMvc(b =>
    {
        b.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
        b.MapODataServiceRoute("odata", "odata", ODataConfig.GetEdmModel());
    });
}
Oscar Llop
  • 191
  • 1
  • 4

1 Answers1

23

I was having same issue after upgrading to .net core 2.2 and found that .net core 2.2 has enabled endpoint routing by default and they have backward capability to disable it like this. It worked for me .

services.AddMvc(options =>
                {
                  options.EnableEndpointRouting = false;
                })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2));
Ankit Patel
  • 603
  • 1
  • 7
  • 9
  • 1
    It's correct, but i'm looking for a solution without disabling the new Endpoint Routing. Is it possible? – Oscar Llop Jun 13 '19 at 11:17
  • 1
    I am also looking for an answer on this. I want to be able to use Odata for my reads "gets" and regular routing from my posts / puts / deletes. – JDBennett Jun 13 '19 at 12:42
  • I see use of CompatibilityVersion.Version_2_1 as per https://github.com/OData/WebApi/issues/1707 – David Burg Feb 05 '20 at 22:50