I don't think there is a quick api method to get the content from routesegment, unfortunately. unless you know the parent node, GetBySegment can be used (as you did)
Thanks Quan ... I kind of guessed so (funny enough the question was implecitly target to you Quan :)) after having spend our of investigation (and reflection) trying to figure it out.
I find using Find is less complex than using ICatalogSystem so I think we will go with that. Here is that solution for future reference (note caching is done by decorator):
public class EntrySegmentToReferenceService : IEntrySegmentToReferenceService
{
private readonly IClient searchClient;
public EntrySegmentToReferenceService(IClient searchClient)
{
this.searchClient = searchClient;
}
public ContentReference GetEntryReferenceBySegment(string routeSegment)
{
var query = searchClient.Search<EntryContentBase>()
.Filter(x => x.RouteSegment.Match(routeSegment))
.Select(x => x.ContentLink)
.Take(1);
var result = query.GetResult().FirstOrDefault();
return result ?? ContentReference.EmptyReference;
}
}
Frankly I think this is a direct query is justified - CatalogItemSeo has the proper index so querying by the UriSegment would be a breeze. You can even add some caching to make it more effective (with depency on the content link)
Of course if you are happy with querying Find, there's no need to write new code.
I have taken over a solution that have custom routing not using the HierarchicalCatalogPartialRouter, but is based on code/sku in the url which is used to identify the entries. It is not really using a hierarchical structure and RouteSegment is not really used for routing at the moment :)
Now the customer wants to use the .RouteSegment (Name in url) instead. And as the RouteSegment is still unique (within the single catalog we have) we should be fine.
How do I fetch an entry using the RouteSegment alone (and language) without the need to use ICatalogSystem or other direct database services.
For now I have gone with using Search & Navigation (read: Find) to fetch the ContentLink by routesegment (sprinkled with some cache magic) which works fine but seems a bit wrong nevertheless.