Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Get entry from RouteSegment

Vote:
 

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.

  • ICatalogSystem seems to be used to make sure that the RouteSegment is unique when publishing entries (the IValidate for entries will only check for uniqueness between siblings).
  • I tried to use IContentLoader.GetBySegment using the catalog as parentlink. It works for some entries but not for all ...

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. 

#299574
Apr 05, 2023 8:08
Vote:
 

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) 

#299577
Apr 05, 2023 9:17
Vote:
 

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;
        }
    }
#299579
Apr 05, 2023 10:15
Vote:
 

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. 

#299580
Apr 05, 2023 11:29
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.