Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

Asp.Net Core 3.1 OData doesn't work with attribute routing and function with parameters

Is it possible to achieve the following route with Asp.Net Core 3.1 and Microsoft.AspNetCore.OData version 7.3.0:

/ParentEntity({id})/Children(OnlyActive={onlyActive})

What I've achieved:

The controller's method:

[HttpGet]
[ODataRoute("ParentEntity({id})/Children")]
public async Task<IActionResult> Children([FromODataUri] string id)
{
    // some data fetching operations.
}

With the following model configuration:

builder.EntityType<ParentEntity>().Function("Children")
                .ReturnsCollectionFromEntitySet<ChildEntity>("ChildEntity");

Works fine and I'm can get id parameter in Children method.

But when I'm trying to do something like:

[HttpGet]
[ODataRoute("ParentEntity({id})/Children(OnlyActive={onlyActive})")]
public async Task<IActionResult> Children([FromODataUri] string id, [FromODataUri] bool onlyActive)
{
   // some data fetching operations.
}

With model configuration:

FunctionConfiguration getFunc = builder.EntityType<ParentEntity>().Function("Children")
                    .ReturnsCollectionFromEntitySet<ChildEntity>("ChildEntity");

getFunc.Parameter<bool>("OnlyActive");

I'm getting the error:

Microsoft.OData.ODataException: 'Bad Request - Error in query syntax'

Is it possible to use Odata functions this way? Or should I try another approach?

question from:https://stackoverflow.com/questions/65885689/asp-net-core-3-1-odata-doesnt-work-with-attribute-routing-and-function-with-par

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to achieve this behavior by specifying 'Deafult' namespace in path in ODataRoute attribute.

After a lot of trying this worked for me:

[HttpGet]
[ODataRoute("ParentEntity({id})/Default.Children(OnlyActive={onlyActive})")]
public async Task<IActionResult> Children([FromODataUri]string id, [FromODataUri]bool onlyActive)

Model configuration:

FunctionConfiguration getFunc = builder.EntityType<ParentEntity>().Function("Children")
                    .ReturnsCollectionFromEntitySet<ChildEntity>("ChildEntity");

getFunc.Parameter<bool>("OnlyActive");

I found the example of bound function with parameters in this tutorial by Sam Xu.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...