[英]How to add custom function to AbpODataEntityController?
In a recent post , there is some suggestion about how to add a custom function to an AbpODataEntityController
. 在最近的一篇文章中 ,有一些关于如何向
AbpODataEntityController
添加自定义函数的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
. 例如,在我的
AbpODataEntityController
,有两个名为Test
和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: 我的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");
The simple function Test
is OK. 简单的功能
Test
就可以了。 It gives a result as: 它给出了一个结果:
{"@odata.context":" http://localhost:21021/odata/ $metadata#Edm.String","value":"inanc"}
{“@ odata.context”:“ http:// localhost:21021 / odata / $ metadata#Edm.String”,“value”:“inanc”}
But the function with a parameter with name param
doesn't work. 但是带有名称
param
的参数的函数不起作用。 I get 404 error. 我收到404错误。
I call the method by http://localhost:21021/odata/towns/EchoTest?param=foo . 我通过http:// localhost:21021 / odata / towns / EchoTest?param = foo调用了该方法。
Where did I go wrong? 我哪里做错了? I should have some custom function that accepts parameters.
我应该有一些接受参数的自定义函数。
Thank you. 谢谢。
You are missing .Collection
: 你错过了。
.Collection
:
builder.EntityType<Town>().Collection
.Function("EchoTest")
.Returns<IActionResult>()
.Parameter<string>("param");
The correct URL: http://localhost:21021/odata/Towns/Default.EchoTest(param='foo') 正确的URL: http:// localhost:21021 / odata / Towns / Default.EchoTest(param ='foo')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.