Route Return OData 404 Not Found

I started incorporating OData into my WebAPi2 project (currently hosted in IIS8 Express on my dev machine). My OData configuration class is as follows:

public class ODataConfig { private readonly ODataConventionModelBuilder modelBuilder; public ODataConfig() { modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet<Category>("Category"); } public IEdmModel GetEdmModel() { return modelBuilder.GetEdmModel(); } } 

Then I added the following to my WebApiConfig class:

 ODataConfig odataConfig = new ODataConfig(); config.MapODataServiceRoute( routeName: "ODataRoute", routePrefix: "MyServer/OData", model: odataConfig.GetEdmModel(), defaultHandler: sessionHandler ); 

And it started with a basic controller and just one action, for example:

 public class CategoryController : ODataController { [HttpGet] public IHttpActionResult Get([FromODataUri] int key) { var entity = categoryService.Get(key); if (entity == null) return NotFound(); return Ok(entity); } } 

Then in my HttpClient the request URL looks like this: MyServer / OData / Category (10)

However, I get the following error:

 {"Message":"No HTTP resource was found that matches the request URI 'http://localhost/MyServer/OData/Category(10)'.","MessageDetail":"No type was found that matches the controller named 'OData'."} 

What am I missing here?

EDIT

If I set routePrefix to null or 'odata' and change the request URL accordingly, the request works fine. So this means that I cannot have a route prefix like "myServer / odata".

Is this the standard OData naming convention? And if so, can this be overridden?

+7
c # odata asp.net-web-api
source share
2 answers

I use the same WebApiConfig.Register() method, which is included in the web API project by default and is passed using the following:

 var builder = new ODataConventionModelBuilder(); // OData entity sets.. builder.EntitySet<Seat>("Seats"); builder.EntitySet<Table>("Tables"); // Configure the Route config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel()); 

The first parameter is a friendly name, the second is what you need! You can change this to whatever you want.

UPDATE: if you are using OData V4, routing is initialized as follows:

config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());

If you use V4, attribute-based routing based on the method is now available (think Nancy's style)

You can use this in the OWIN start class as well as in Global.asax. Anyway, this works great for me.

Contact: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/creating-an-odata-endpoint

+1
source share

It may be too late, but for everyone who ends here ...

I don’t think the problem is odata. Perhaps you are using the default routing foul, because the message “Type not found, which matches the controller named“ OData ”, assumes that http://localhost/MyServer/OData/Category(10) routed using

 routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters 

therefore, he is looking for a controller called ODataController with a Category action. You must define "localhost / MyServer" as the root from which routing is applied. Unfortunately, I can’t suggest how you can do this, but hopefully this indicates that you are in the right direction.

+1
source share

All Articles