Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData routes return 404 Not Found

I've started including OData in my WebAPi2 project (currently hosted in IIS8 Express on my dev machine). My OData config class looks like this:

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 in my WebApiConfig class:

ODataConfig odataConfig = new ODataConfig();

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

And started with a basic controller and just one action, like this:

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'm getting 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 the routePrefix to null or 'odata' and change my request url accordingly, the request works fine. So this means that I can't have a route prefix like 'myServer/odata'.

Is this OData standard naming convention? And if yes, can it be overridden?

like image 541
Ivan-Mark Debono Avatar asked Nov 11 '14 12:11

Ivan-Mark Debono


1 Answers

This is probably too late, but for anyone else that ends up here...

I don't think the problem is odata. Perhaps you're running foul of the default routing as the message "No type was found that matches the controller named 'OData'" suggests that http://localhost/MyServer/OData/Category(10) is being routed using

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

so it's looking for a controller called ODataController with an action "Category". You need to define "localhost/MyServer" as the root from which the routing is applied. Unfortunately I can't suggest how you might do that but hopefully this points you in the right direction.

like image 138
christutty Avatar answered Oct 14 '22 11:10

christutty