TheBoubou
TheBoubou

Reputation: 19903

OData, method get with key not found

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

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());
    }
}

Upvotes: 1

Views: 1635

Answers (2)

Sam Xu
Sam Xu

Reputation: 3380

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)

https://learn.microsoft.com/en-gb/odata/webapi/built-in-routing-conventions lists the routing conventions used in Web API OData. Hope it can help you. Thanks.

Upvotes: 0

TomDoesCode
TomDoesCode

Reputation: 3681

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

Get([FromODataUri] int key)

Upvotes: 1

Related Questions