控制器中操作的路径模板不是有效的OData路径模板 [英] The path template on the action in controller is not a valid OData path template

查看:138
本文介绍了控制器中操作的路径模板不是有效的OData路径模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误:

控制器"Clients"中操作"GetClients"上的路径模板"GetClients()"不是有效的OData路径模板.找不到段"GetClients"的资源.

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'.

我的控制器方法如下

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

我的 WebAPIConfig 文件具有

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

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

我希望能够像这样调用odata rest api:

I am hoping to be able to call the odata rest api like this:

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

知道我在做什么错吗?

推荐答案

您甚至不需要添加这样的函数即可获得实体.

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

builder.EntitySet<ClientModel>("Clients")

是您所需要的.

然后将您的操作写为:

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

这是行得通的.上面的方法不起作用:

This is what worked. The above did not work:

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

然后获取请求

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

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

将起作用.

更新:使用未绑定的函数返回许多ClientModel.

Update: Use unbound function to return many ClientModels.

以下代码适用于v4.对于v3,您可以使用操作.

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

发送如下请求:

GET ~/FunctionName(Id=5)

这篇关于控制器中操作的路径模板不是有效的OData路径模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆