How to add custom function to AbpODataEntityController?

删除回忆录丶 提交于 2019-12-11 16:47:22

问题


In a recent post, there is some suggestion about how to add a custom function to an AbpODataEntityController. I have tried but I can't achieve what I want.

For example, in my AbpODataEntityController, there are two methods called Test and EchoTest.

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

And my Startup.cs has:

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

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

The simple function Test is OK. It gives a result as:

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

But the function with a parameter with name param doesn't work. I get 404 error.

I call the method by http://localhost:21021/odata/towns/EchoTest?param=foo.

Where did I go wrong? I should have some custom function that accepts parameters.

Thank you.


回答1:


You are missing .Collection:

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

The correct URL: http://localhost:21021/odata/Towns/Default.EchoTest(param='foo')



来源:https://stackoverflow.com/questions/55164203/how-to-add-custom-function-to-abpodataentitycontroller

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