user7932844
user7932844

Reputation: 351

ASP.NET Core 2, OData v4 Attribute Routing

Is it possible to achieve the below routes using the asp.net core 2, web api, odata v4.

By using the attribute routing, declaring functions and customizing routes?

Upvotes: 0

Views: 4352

Answers (2)

Leo
Leo

Reputation: 1

I was also able to achieve it using attribute routing alone too.

app.UseMvc(b =>
{
    b.MapODataServiceRoute("odata", "api/v1", GetEdmModel());
});

In the controller:

using Microsoft.AspNet.OData.Routing;

[EnableQuery]
[ODataRoute("content/{contentId}")]
public IActionResult Get(string contentId)

Upvotes: 0

user7932844
user7932844

Reputation: 351

After long experiment using attribute routing, functions and actions, the above can be achieved using the OData functions.

Below is the code for it.

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

builder.EntitySet<Product>("Product");
builder.EntityType<Product>().Function("Users").Returns<Users>();
builder.EntityType<Product>().Function("Companies").Returns<Companies>();

app.UseMvc(routebuilder =>
{
     routebuilder.MapODataServiceRoute( "OData", "odata", GetEdmModel());
     routebuilder.EnableDependencyInjection();
}

Refer the below for more understanding.

https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions

Upvotes: 1

Related Questions