ASP.NET Core OData 8: allow deep navigation

We’re using odata2ts as our client side library. As stated on it’s docs:

OData exposes and advertises entry points, most often entity sets (e.g. /People). We can traverse to a specific entity of that collection by its key(s) (e.g. /People(‘russelwhyte’)) and use its navigation properties to traverse ever deeper into the service (e.g. /People(‘russelwhyte’)/BestFriend/Trips). There is no defined end to this kind of navigation.

So given the following EF classes (generated by EF Core Power Tools, SQL Server 2012 – database-first approach), registered as entity sets with pluralized names on the EDM

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Service
{
int ServiceId { get; set; }
ICollection<ServiceArticle> ServiceArticle { get; set; }
}
public class ServiceArticle
{
int ServiceArticleId { get; set; }
int ServiceId { get; set; }
Service Service { get; set; }
int ArticleId { get; set; }
Article Article { get; set; }
}
public class Article
{
int ArticleId { get; set; }
}
</code>
<code>public class Service { int ServiceId { get; set; } ICollection<ServiceArticle> ServiceArticle { get; set; } } public class ServiceArticle { int ServiceArticleId { get; set; } int ServiceId { get; set; } Service Service { get; set; } int ArticleId { get; set; } Article Article { get; set; } } public class Article { int ArticleId { get; set; } } </code>
public class Service
{
    int ServiceId { get; set; }
    ICollection<ServiceArticle> ServiceArticle { get; set; }
}

public class ServiceArticle
{
    int ServiceArticleId { get; set; }

    int ServiceId { get; set; }
    Service Service { get; set; }
    int ArticleId { get; set; }
    Article Article { get; set; }
}    

public class Article
{
    int ArticleId { get; set; }
}

Although it doesn’t seem too useful, the odata2ts service allows to call

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var article = await odata2tsService.Services(15).ArticleService(20).Article().query();
</code>
<code>var article = await odata2tsService.Services(15).ArticleService(20).Article().query(); </code>
var article = await odata2tsService.Services(15).ArticleService(20).Article().query();

which translates to the request

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/Services(15)/ArticleService(20)/Article
</code>
<code>/Services(15)/ArticleService(20)/Article </code>
/Services(15)/ArticleService(20)/Article

as you’d expect.

Now the question is, how do we allow this from an API standpoint?

We’ve got endpoints like the following convention to get each of an EntityType properties, so there’s

ServicesController.cs:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[HttpGet]
[EnableQuery]
public IActionResult GetArticleService([FromRoute] int key)
</code>
<code>[HttpGet] [EnableQuery] public IActionResult GetArticleService([FromRoute] int key) </code>
[HttpGet]
[EnableQuery]
public IActionResult GetArticleService([FromRoute] int key)

and Get() and Get([FromRoute] int key) for each of the involved entities on their respective controllers. What are we missing to achieve desired result? Given Sam Xu comment here it would seem that this is covered by convention, but we haven’t been able to get this to work by adding a:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[HttpGet]
[EnableQuery]
public IActionResult GetArticleFromService([FromRoute] int key, [FromRoute] int relatedKey)
</code>
<code>[HttpGet] [EnableQuery] public IActionResult GetArticleFromService([FromRoute] int key, [FromRoute] int relatedKey) </code>
[HttpGet]
[EnableQuery]
public IActionResult GetArticleFromService([FromRoute] int key, [FromRoute] int relatedKey)

It seems you want to get a convention as:

~/{entityset}({key})/{navigationProperty}({key})

In the convention, it seems no such convention built by default.

You can build the convention by yourself using any ways, or use the attribute routing to achieve the goal.

I create a sample for your reference at here. Please leave any comments in the repo by creating issues and love to see any contributions by pull request.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật