如何向 AbpODataEntityController 添加自定义函数?

How to add custom function to AbpODataEntityController?

在最近的 post 中,有一些关于如何向 AbpODataEntityController 添加自定义函数的建议。我已经尝试过了,但我无法达到我想要的效果。

例如,在我的AbpODataEntityController中,有两个方法叫做TestEchoTest

public class TownsController : AbpODataEntityController<Town>, ITransientDependency
{
    public TownsController(IRepository<Town> repository) : base(repository)
    {
    }

    // [HttpPost]
    public string Test()
    {
        return "inanc";
    }

    // [HttpPost]
    // [HttpGet]
    public IActionResult EchoTest([FromODataUri] string param)
    {
        return Ok(string.Format("The param was {0}", param));
    }
}

我的 Startup.cs 有:

builder.EntityType<Town>().Collection
    .Function("Test")
    .Returns<string>();
    //.Parameter<string>("param");

builder.EntityType<Town>()//.Action("stringTest")
    .Function("EchoTest")
    .Returns<IActionResult>()
    .Parameter<string>("param");

简单的功能Test就可以了。结果为:

{"@odata.context":"http://localhost:21021/odata/$metadata#Edm.String","value":"inanc"}

但是参数名称为 param 的函数不起作用。我收到 404 错误。

我通过http://localhost:21021/odata/towns/EchoTest?param=foo调用方法。

我哪里错了?我应该有一些接受参数的自定义函数。

谢谢。

你错过了.Collection:

builder.EntityType<Town>().Collection
    .Function("EchoTest")
    .Returns<IActionResult>()
    .Parameter<string>("param");

正确的URL:http://localhost:21021/odata/Towns/Default.EchoTest(param='foo')