How to add custom function to AbpODataEntityContro

2019-08-29 08:41发布

问题:

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