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())
};
}
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: