1
votes

With the code below, I can hit (using Fiddler):

  • GetCustomers via GET: odata/Customers
  • Post(CustomerModel customer) via POST: odata/Customers
  • Delete via DELETE: odata/Customers(5)

The delete method look like :

public IHttpActionResult Delete([FromODataUri] int key)
{
    Console.WriteLine(key);
}

I hit the method and I get the key, no problem.

But I don't hit the get method with the key (no problem with the get method without the key, I get the full list) :

// GET: odata/Customers(5)
public IHttpActionResult GetCustomer([FromODataUri] int key)
{
    Console.WriteLine(key);
}

I get this error (Response headers via Fiddler): HTTP/1.1 404 Not Found

The WebApiConfig is :

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<CustomerModel>("Customers");
        builder.EntitySet<EmployeeModel>("Employees");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: "odata",
            model: builder.GetEdmModel());
    }
}
2
Can you try calling the method GetCustomers([FromODataUri] int key) ?TomDoesCode
I tried, same result ..not found.Kris-I
How about Get([FromODataUri] int key) ?TomDoesCode
Works. Method named "Get" and call with odata/Customers(2), that's strange no ? And the name I gave at controller creation by VSKris-I
I've posted that as an answer so that it can be seen for other people. It is strange but things do start to get difficult when using the builder to setup OData instead of entity!TomDoesCode

2 Answers

1
votes

The method name needs to be Get to be picked up by the OData routing:

Get([FromODataUri] int key)
0
votes

By Web API OData convention, it should support the following two rules:

  1. HttpMethodName + entityTypeName
  2. HttpMethodName

Convention #1 has high priority than convention #2.

Based on the conventions, you will get 404-NotFound if you only define the following actions in the controller:

GetCustomer([FromODataUri] int key)
GetCustomers([FromODataUri] int key)

Otherwise, it should work if you define at least one of the following actions in the controller:

GetCustomerModel([FromODataUri] int key)
Get([FromODataUri] int key)

http://odata.github.io/WebApi/#03-02-built-in-routing-conventions lists the routing conventions used in Web API OData. Hope it can help you. Thanks.