Hello,
Current version of EPiServer.CMS = 11.10.6
I have a structure that looks something like figure1.
Each "MyPage" will have a unique code/id in it's "Name in URL" (URLSegment).
This is a multisite multilingual environment.
What I would like to achieve is that all "MyPage" gets a url directly under "ContainerPage1", in this format:
mypage4 = my-domain/ContainerPage1/unique-code-from-mypage4/the-page-name-from-mypage4/
mypage1 = my-domain/ContainerPage1/unique-code-from-mypage1/the-page-name-from-mypage1/
Figure 1
SiteStartPage
- ContainerPage1
-- MyPage1
-- ContainerPage2
--- MyPage2
--- MyPage3
--- ContainerPage3
---- MyPage4
---- MyPage5
SiteStartPage2
...
Tips are gratefully received :)
So far I have been playing around with something like this, it seems to "work" ( the part after /unique-code-from-mypage4/ can be anything in this ), but I'm pretty sure there are better ways and that there are tons of possible problems here ;). Haven't any real experince doing this partial router stuff.
public class MyPagePartialRouter : IPartialRouter<ContainerNode, MyPage>
{
...
private IContentLoader _contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
private IUrlSegmentGenerator _urlSegmentGenerator = ServiceLocator.Current.GetInstance<IUrlSegmentGenerator>();
public object RoutePartial(ContainerNode content, SegmentContext segmentContext)
{
...
var nextSegment = segmentContext.GetNextValue(segmentContext.RemainingPath);
var theSegmentWithUniqueCode = HttpUtility.UrlDecode(nextSegment.Next);
var myPageInstanceFound = _contentLoader.GetDescendents(_refefToContainerPage1)
.Select(x =>
{
if (_contentLoader.TryGet<MyPage>(x, out var myPageInstance))
if (myPageInstance.URLSegment == theSegmentWithUniqueCode)
return myPageInstance;
return null;
})
.FirstOrDefault(x => x != null);
if (myPageInstanceFound == null)
return null;
segmentContext.RemainingPath = ""; // I had to do like this otherwise I got a 404
segmentContext.RoutedContentLink = myPageInstanceFound.ContentLink;
return myPageInstanceFound;
}
public PartialRouteData GetPartialVirtualPath(MyPage content, string language, RouteValueDictionary routeValues, RequestContext requestContext)
{
var urlSegment = _urlSegmentGenerator.IsValid(content.URLSegment)
? content.URLSegment
: _urlSegmentGenerator.Create(content.URLSegment);
return new PartialRouteData()
{
BasePathRoot = _refefToContainerPage1,
PartialVirtualPath = string.Format("{0}/{1}/", urlSegment, _urlSegmentGenerator.Create(content.Name))
};
}
}