November Happy Hour will be moved to Thursday December 5th.

“Partial Routing”: How to get the current page reference?

Vote:
 

I have used partial routing concept for URL shortening and it is working as expected. But for getting the page reference, I have passed the “Page ID” value into the URLsegment and get the page values (refer the code below). Instead of passing page id into the URL, Is there any other way to get the page reference of current routed page?

URL format:


http://testsite.org/article/{page name}/{page ID}

Code which I have used:

 public object RoutePartial(HomePage content, SegmentContext segmentContext)
        {
            //Expected format is article-category/artilepagename/2456

            //Vaerify the current reference is start page
            if (!content.ContentLink.CompareToIgnoreWorkID(ContentReference.StartPage))
            {
                return null;
            }

            //Get the category path value
            var nextSegment = segmentContext.GetNextValue(segmentContext.RemainingPath);
            var urlSegment = nextSegment.Next;
            if (string.IsNullOrEmpty(urlSegment))
            {
                return null;
            }
            //Get the remaining path value (pagename/pageid)
            var namePart = segmentContext.GetNextValue(nextSegment.Remaining);
            var numberPart = segmentContext.GetNextValue(namePart.Remaining);
            if (!string.IsNullOrEmpty(numberPart.Next))
            {
                //get the page reference
                  if(
                      (urlSegment == "article")               
                    )
                {
                var pageRef = new PageReference(numberPart.Next);
                var page = DataFactory.Instance.GetPage(pageRef) as ArticlePage;                    
                     if (page != null && page.ContentLink!=null)
                    {
                        //Update RemainingPath on context.
                        segmentContext.RemainingPath = numberPart.Remaining;
                        segmentContext.RoutedContentLink = page.ContentLink;
                        return page;
                    }
                }
            }
           


            return content;
        }

 

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

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

            if (PageEditing.PageIsInEditMode)
            {
                return null;
            }

            return new PartialRouteData
            {
                BasePathRoot = ContentReference.StartPage,
                PartialVirtualPath = String.Format("{0}/{1}/{2}/", "article", content.URLSegment, content.ContentLink.ID.ToString())
               
            };
        }

 

#115444
Jan 15, 2015 9:52
Vote:
 
#115663
Jan 15, 2015 15:34
Vote:
 

To resolve the issue, I have used Dynamic Data Store to save and get the page id using URLsegment.

I would like to know whether this soluion is the best practices ?

#116045
Jan 22, 2015 10:12
* 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.