16
votes

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

1 Answers

23
votes

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));