The path template in action in the controller is not a valid OData path template

I get the following error:

The GetClients () path template in the GetClients action in the Clients controller is not a valid OData path template. Resource not found for GetClients segment.

My controller method is as follows

public class ClientsController : ODataController
{
    [HttpGet]
    [ODataRoute("GetClients(Id={Id})")]
    public IHttpActionResult GetClients([FromODataUri] int Id)
    {
        return Ok(_clientsRepository.GetClients(Id));
    }
}

My webapiconfig file has

builder.EntityType<ClientModel>().Collection
       .Function("GetClients")
       .Returns<IQueryable<ClientModel>>()
       .Parameter<int>("Id");

config.MapODataServiceRoute(
    routeName: "ODataRoute",
    routePrefix: "odata",
    model: builder.GetEdmModel());

I hope you can call adat api as follows:

http://localhost/odata/GetClients(Id=5)

Any idea what I'm doing wrong?

+4
source share
2 answers

You do not even need to add such a function to get an object.

builder.EntitySet<ClientModel>("Clients")

- , .

:

public IHttpActionResult GetClientModel([FromODataUri] int key)
{    
      return Ok(_clientsRepository.GetClients(key).Single());
}

, . :

public IHttpActionResult Get([FromODataUri] int key)
{    
    return Ok(_clientsRepository.GetClients(key).Single());
}

Get

http://localhost/odata/Clients(Id=5)

http://localhost/odata/Clients(5)

.

. unbound ClientModels.

v4. v3 .

builder.EntitySet<ClientModel>("Clients");
var function = builder.Function("FunctionName");
function.Parameter<int>("Id");
function.ReturnsCollectionFromEntitySet<ClientModel>("Clients");

, :

[HttpGet]
[ODataRoute("FunctionName(Id={id})")]
public IHttpActionResult WhateverName(int id)
{
    return Ok(_clientsRepository.GetClients(id));
}

, :

GET ~/FunctionName(Id=5)
+6

: [ODataRoute("GetClients(Id={Id})")]

: [ODataRoute("Clients({Id})")]

URL : http://localhost/odata/Clients(Id=5)

-1

Source: https://habr.com/ru/post/1547357/


All Articles