Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No non-OData HTTP route registered

I followed this tutorial to create a WebAPI REST service.

After that, I could load the list of all contacts by pointing at http://baseaddress/api/Contacts.

Then I added the following code in the Register method in WebApiConfig.cs in order to enable an OData endpoint:

config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Contact>("Contacts");
config.MapODataServiceRoute(
    routeName: "OData",
    routePrefix: "odata",
    model: builder.GetEdmModel());

And also added the [EnableQuery] parameter on the Contact.GetContacts() method. That way, I am able to query for particular contacts like this:

http://baseaddress/odata/Contacts?$filter=startswith(Name,'A')

and it works like charm.

Unfortunately, when I put the [EnableQuery], the WebAPI endpoint stops working, showing instead the following error:

No non-OData HTTP route registered.

in System.Web.OData.Extensions.HttpConfigurationExtensions.GetNonODataRootContainer(HttpConfiguration configuration)
in System.Web.OData.Extensions.HttpRequestMessageExtensions.GetRootContainer(HttpRequestMessage request, String routeName)
in System.Web.OData.Extensions.HttpRequestMessageExtensions.CreateRequestScope(HttpRequestMessage request, String routeName)
in System.Web.OData.Extensions.HttpRequestMessageExtensions.CreateRequestContainer(HttpRequestMessage request, String routeName)
...

What should I do to fix this?

like image 638
Teejay Avatar asked Feb 28 '17 22:02

Teejay


5 Answers

I encountered this problem, and since I'm working with dependency injections I managed to solve this by adding GlobalConfiguration.Configuration.EnableDependencyInjection() to my startup.cs

ex.

using System.Web.OData.Extensions;
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        GlobalConfiguration.Configuration.EnableDependencyInjection();
    }
}
like image 140
ZarX Avatar answered Nov 02 '22 06:11

ZarX


Adding config.EnableDependencyInjection() in Startup.cs worked for me.

var config = new HttpConfiguration();

config.EnableDependencyInjection();
like image 34
jt03 Avatar answered Nov 02 '22 04:11

jt03


The key with this issue is to use .EnableDependencyInjection() on Configure method in Startup.cs

If you are using ASP.net Core endpoint routing (recommended if you have at least .net core 3.0 and Microsoft.AspNetCore.OData v7.4.0)

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();

    endpoints.Select().Filter().OrderBy().Count().MaxTop(10);
    endpoints.EnableDependencyInjection();//This guy solves the problem    
    endpoints.MapODataRoute("odata", "odata", GetEdmModel());
});

Otherwise if you are using MVC routing (only way available prior .net core 3.0 and Microsoft.AspNetCore.OData v7.4.0)

app.UseMvc(routeBuilder =>
{
    routeBuilder.Select().Filter().OrderBy().Count().MaxTop(10);
    routeBuilder.EnableDependencyInjection();//This guy solves the problem
    routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel());
});

Further reading: https://devblogs.microsoft.com/odata/enabling-endpoint-routing-in-odata/

like image 5
gsubiran Avatar answered Nov 02 '22 04:11

gsubiran


I encountered this problem before and by the adding the line below it worked for me

protected void Application_Start()
            {  
         GlobalConfiguration.Configuration.EnableDependencyInjection();
             ....  }
like image 2
M. Abouzeid Avatar answered Nov 02 '22 04:11

M. Abouzeid


I got this error after updating my WebApi project dependencies (NuGet) to:

  • Microsoft.AspNet.OData, version="7.0.1"
  • Microsoft.OData.Core, version="7.5.0"
  • Microsoft.OData.Edm, version="7.5.0"
  • Microsoft.Spatial, version="7.5.0"

After downgrading to the versions I used before, the error was gone again:

  • Microsoft.AspNet.OData, version="5.8.0"
  • Microsoft.OData.Core, version="6.19.0"
  • Microsoft.OData.Edm, version="6.19.0"
  • Microsoft.Spatial, version="6.19.0"
like image 2
Alexander Avatar answered Nov 02 '22 04:11

Alexander