Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use 'Microsoft.AspNet.OData.Routing.ODataRoute' with Endpoint Routing.' Exception with ASP Net Core 2.2

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());
    });
}
like image 332
Oscar Llop Avatar asked Dec 13 '18 10:12

Oscar Llop


People also ask

What is OData routing in ASP NET Core?

In the previous version of ASP.NET Core OData, such as 6.x and 7.x version, the OData routing is IRouter-based Web API Routing, that is, OData router is a Router implementing IRouter interface. Even in the ASP.NET Core OData 7.x Endpoint Routing, it is also related to the IRouter routing.

What is odatarouteattribute in ASPnet WebAPI?

Applies To: OData AspNet WebApi V7 OData AspNet WebApi V6 Same as Web API, Web API OData supports a new type of routing called attribute routing. It uses two Attributes to find controller and action. One is ODataPrefixAttribute, the other is ODataRouteAttribute.

Does OData 7 require endpoint routing?

But for those who have existing APIs or were planning to develop new APIs leveraging endpoint routing, the OData 7.3.0 release didn’t quiet meet their expectations without having to disable endpoint routing. Understandably this was quite a trade off between leveraging the capabilities of endpoints routing versus being able to use OData.

What is odataroutingconventions in web API OData?

In Web API OData, attribute routing is combined with convention routing by default. ODataRoutingConventions provides two methods to register routing conventions: As the name implies, the first one creates a mutable list of the default OData routing conventions with attribute routing enabled, while the second one only includes convention routing.


1 Answers

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));
like image 90
Ankit Patel Avatar answered Oct 20 '22 16:10

Ankit Patel