No non-OData HTTP route registered
Solution 1
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();
}
}
Solution 2
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/
Solution 3
Adding config.EnableDependencyInjection()
in Startup.cs
worked for me.
var config = new HttpConfiguration();
config.EnableDependencyInjection();
Solution 4
I encountered this problem before and by the adding the line below it worked for me
protected void Application_Start()
{
GlobalConfiguration.Configuration.EnableDependencyInjection();
.... }
Solution 5
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"
Related videos on Youtube
Teejay
C# / VB.net developer Freelance web-designer Hobbyist photographer
Updated on September 14, 2022Comments
-
Teejay almost 2 years
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 theContact.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?
-
Wade Bee about 7 yearsIf you aren't using GlobalConfiguration, modify the above like:
var httpConfiguration = new HttpConfiguration();
httpConfiguration.EnableDependencyInjection();
var request = new HttpRequestMessage(HttpMethod.Get, (Uri)Request.Url);
request.SetConfiguration(httpConfiguration);
</code> -
bkwdesign over 6 yearsI'm NOT using DI, but now I have this code in my
App_Start/WebApiConfig.cs
- which indeed makes it work.. but it makes no sense -
Jon H over 5 yearsI really didn't think this would fix my issue - but it did. I hadn't twigged this isn't just a pipeline or MVC thing it is in the odata namespace, so no danger of side effects.
-
David Burg over 5 yearsThis is because with the new OData version the dependency injection model has changed. The suggested httpConfiguration.EnableDependencyInjection(); above allow the code to work with new OData version.