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

Routing Issue in Episerver

Vote:
 

Hi ,

We are using Episerver CMS 10 .

I have a news for which the URL is domain/nyheter/<newsname>
and we have implemented partial routing for that as below :

public object RoutePartial(ContainerBasePageType content, SegmentContext segmentContext) {

//Use helper method GetNextValue to get the next part from the URL
SegmentPair nextSegment = segmentContext.GetNextValue(segmentContext.RemainingPath);
NewsPageType newsContent = null;

// Find the global newspage that now has a url under local newscontainer.
if(!string.IsNullOrEmpty(nextSegment.Next)) {
string newsPageUrlSegment = nextSegment.Next.TrimEnd(new[] { '/' });

var companyPickerRef = ContentReference.StartPage;
var globalStartPage = this.contentLoader.GetChildren<MasterStartPageType>(companyPickerRef).FirstOrDefault();
if(globalStartPage != null) {
try {
newsContent = this.searchRepository.SearchForRoutedGlobalNews(newsPageUrlSegment, globalStartPage.ContentLink);
} catch(Exception ex) {
this.log.Error("Global news routing failed. Crash in Episerver Find routines" + ex);
}
}

if(newsContent != null) {
//Update RemainingPath so the part that we have handled is removed.
segmentContext.RemainingPath = nextSegment.Remaining;
segmentContext.RoutedContentLink = newsContent.ContentLink;
}
}

return newsContent;
}


//Makes every link of a newspagetype point to the local news page instead of global thing.
public PartialRouteData GetPartialVirtualPath(NewsPageType content, string language, RouteValueDictionary routeValues, RequestContext requestContext) {

var contentLink = requestContext.GetRouteValue("node", routeValues) as ContentReference;

if(!content.ContentLink.CompareToIgnoreWorkID(contentLink) || PageEditing.PageIsInEditMode || HttpContext.Current == null) {
return null;
}

ContentReference newsContainerRef = null;
PageData selectedCompanyStartPage = this.coreHelper.GetStartPageForRoutedContent();
if(selectedCompanyStartPage is LocalStartPageType) {
newsContainerRef = ((LocalStartPageType)selectedCompanyStartPage).NewsContainer;
}

return new PartialRouteData {
BasePathRoot = newsContainerRef,
PartialVirtualPath = content.urlsegment
};
}
}
}

but i want that it should follow hierachial structure for news
domain/nyheter/foldername/newsname so i have made changes in GetPartialVirtualPath as follows 

var parentPage = contentLoader.Get<IContent>(content.ParentLink); //

PartialVirtualPath = String.Format("{0}/{1}/",
parentPage.Name,
HttpUtility.UrlPathEncode(content.Name))

but it is still giving 404 error

Thanks in advance !

Regards,

Titiksha Mahimker.

#203209
Edited, Apr 11, 2019 15:06
Vote:
 

Hi!

Do you really want to use page name as URL segment? If an editor changes the name of the page you will end up with 404 requests if Google and other search engines have indexed that page. A page's URL should be changed with caution and it should always be handled with redirects if you want your site to score high in search engines.

However, if you make these changes to GetPartialVirtualPath, you also have to make changes to RoutePartial since you can't fetch a page by URL segment anymore. Or does searchRepository.SearchForRoutedGlobalNews already fetch pages by page name?

And may I also suggest that you use "Insert/Edit code sample" button when you post sample code and select C# as language, otherwise it's very hard to read. :)

#203239
Edited, Apr 12, 2019 14:53
Vote:
 

hi Mattias,

Thanks for the comments :)

I have changed the code from name to "url segment" in
"PartialVirtualPath"

 return new PartialRouteData {
                BasePathRoot = newsContainerRef,
                PartialVirtualPath = String.Format("{0}/{1}/",
                                parentPage.URLSegment,
                                HttpUtility.UrlPathEncode(content.URLSegment))
            };


then when the request comes in it takes ContainerBasePageType as the parent folder of the news so
i took the

string remainingsegment = nextSegment.Remaining.TrimEnd(new[] { '/' });

where the news urlsegment is present
and below is the code for news content

public NewsPageType SearchForRoutedGlobalNews(string globalNewsUrlSegment, ContentReference globalStartPageRef)
{
try
{
var newsHit = this.client.Search<NewsPageType>()
.Filter(x => x.URLSegment.Match(globalNewsUrlSegment))
.Filter(x => x.Ancestors().Match(globalStartPageRef.ToString()))
.StaticallyCacheFor(new TimeSpan(0, 5, 0)) //Cache for 5 minutes
.GetContentResult().FirstOrDefault();

return newsHit;

}
catch (Exception)
{

return null;
}

}

Here the content is loaded but still it gives 404 error.

#203273
Apr 15, 2019 11:24
* 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.