8

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?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

2 Answers2

2

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.

christutty
  • 952
  • 5
  • 12
  • i spent an hour trying to figure out what i did wrong. thanks! – Giovanni Galbo May 22 '17 at 18:14
  • @christutty I just wanted to add to this.... This can be solved by moving your `config.MapODataServiceRoute{...}` implementation above the `routes.Maproute{...}` OR `config.MappHttpRoute{...}` implementation in your WebApiConfig – ganjeii Nov 19 '18 at 23:09
1

I've been using the same WebApiConfig.Register() method that is included by default in the Web API project and passing 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 the one you're after! You can change this to whatever you want.

UPDATE: If you're using OData V4, the routing is initialised like this:

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

If you're using V4, method based routing via the use of attributes is now available (think Nancy style)

You can use this in either an OWIN startup class or the Global.asax. Either way works fine for me.

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

Matthew Merryfull
  • 1,466
  • 18
  • 32