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

北战南征 提交于 2019-12-10 01:59:15

问题


I am getting the following error:

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

My controller method looks like this

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 am hoping to be able to call the odata rest api like this:

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

Any idea what I am doing wrong?


回答1:


You don't even need to add such a function to get an entity.

builder.EntitySet<ClientModel>("Clients")

is all you need.

And then write your action as:

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

Or

This is what worked. The above did not work:

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

Then the Get request

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

or

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

will work.

Update: Use unbound function to return many ClientModels.

The follow code is for v4. For v3, you can use action.

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

Add a method in the controller like:

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

Send a request like:

GET ~/FunctionName(Id=5)



回答2:


This route is incorrect: [ODataRoute("GetClients(Id={Id})")]

It should be: [ODataRoute("Clients({Id})")]

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



来源:https://stackoverflow.com/questions/24602935/the-path-template-on-the-action-in-controller-is-not-a-valid-odata-path-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!