Partial Route - removing a segment from the URL

Vote:
 

Hi team, 

I'm working on partial route in purpose to remove a segment from an URL. 

We have the following structure: 

Landing Page →  Container Page →  Start Page →  Standard Page →  [other pages of type Standard Page]

I need for each page under the ContainerPage to hide its segment from URL.

Example: http://siteHost/landingPage/containerPage/startPage/standardPage1/standardPage2/

Expected to be: http://siteHost/landingPage/startPage/standardPage1/standardPage2/

(no containerPage segment)

There is a partial router, it inherits from

IPartialRouter<LandingPage, BasePage>

where BasePage is a base type for both page types - StartPage and StandardPage. Methods of that interface are implemented: 

object RoutePartial(LandingPage content, SegmentContext segmentContext) { ... }
PartialRouteData GetPartialVirtualPath(BasePage content, string language, RouteValueDictionary routeValues, RequestContext requestContext) { ... }

Router is registered. We get into partial router for pages like following: 

http://siteHost/landingPage/containerPage/startPage

But for the page from example it is not working - we even don't get into the method. 

Did I miss something in the solution? 

Thanks in advance! 

#287977
Edited, Sep 23, 2022 10:05
Vote:
 

We have done something Similar and had to use Search and Navigation to Find the Page to serve full example here 

    public class CampaignRouter : IPartialRouter<InsightLandingPage, CampaignPage>
    {

#if DEBUG
        private readonly TimeSpan _cacheTimespan = new TimeSpan(0, 0, 0);
#else
        private readonly TimeSpan _cacheTimespan = new TimeSpan(0, 30, 0);
#endif
        private ContentReference _container;

        private string _urlSegment;

        public CampaignRouter()
        {
            var settings = ServiceLocator.Current.GetInstance<ISiteSettingsProvider>();
            _container = settings.Current.InsightsContainer;
        }

        public object RoutePartial(InsightLandingPage content, SegmentContext segmentContext)
        {
            if (!content.ContentLink.CompareToIgnoreWorkID(_container))
            {
                return null;
            }

            var nextSegment = segmentContext.GetNextValue(segmentContext.RemainingPath);
            var urlSegment = nextSegment.Next;
            if (string.IsNullOrEmpty(urlSegment))
            {
                return null;
            }

            ArticleBasePage article = null;

            var cacheKey = $"routing-insights-{urlSegment}";

            var cache = ServiceLocator.Current.GetInstance<ICache<ArticleBasePage>>();

            _urlSegment = urlSegment;

            if (cache.IsExists(cacheKey))
            {
                article = cache.Get(cacheKey, GetArticleBasePage);
            }
            else
            {
                article = GetArticleBasePage();
                if (article != null) cache.AddToCache(cacheKey, article, _cacheTimespan);
            }

            if (article != null)
            {
                segmentContext.RemainingPath = nextSegment.Remaining;
                segmentContext.RoutedContentLink = article.ContentLink;
            }

            return article;
        }

        private ArticleBasePage GetArticleBasePage()
        {
            var urlSegment = _urlSegment;

            ArticleBasePage article = null;

            var searchContext = SearchClient.Instance
                .Search<ArticleBasePage>(SearchClient.Instance.Settings.Languages.GetSupportedLanguage("en"))
                .Filter(x => x.URLSegment.MatchCaseInsensitive(urlSegment))
                .Filter(d => d.Ancestors().Match(_container.ID.ToString()))
                .StaticallyCacheFor(this._cacheTimespan)
                .Select(r => r.ContentLink);


            var searchResults = searchContext.GetResult().FirstOrDefault();

            if (!ContentReference.IsNullOrEmpty(searchResults))
            {
                var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
                article = contentRepository.Get<ArticleBasePage>(searchResults);
            }

            return article;
        }

        public PartialRouteData GetPartialVirtualPath(
        CampaignPage content,
        string language,
        RouteValueDictionary routeValues,
        RequestContext requestContext)
        {
            var contentLink = requestContext.GetRouteValue("node", routeValues)
                as ContentReference;

            if (!content.ContentLink.CompareToIgnoreWorkID(contentLink))
            {
                return null;
            }

            if (PageEditing.PageIsInEditMode)
            {
                return null;
            }


            return new PartialRouteData
            {
                BasePathRoot = _container,
                PartialVirtualPath = content.URLSegment
            };
        }
    }
#287980
Sep 23, 2022 12:42
* 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.