In last project I had to prepare a custom Partial Router. The part of the site consists of news home page which contains news articles. But there is also additional container page used by editors to group content. The scenario was very simple – the container page shouldn’t be displayed in URL.
I used the example from EPiServer 9 documentation. My case was very similar so I could reuse almost all of the sample code.

Current page issue

After I run the application and tried open any article I get exception.

partial router page instance exception

The stack trace:

It turns out that router RoutePartial method requires to set two properties on segmentContext object:

  • RemainingPath – the remaining URL segments that have to handled
  • RoutedContentLink – current page reference

I didn’t set the RoutedContentLink property. Because of that, the template was resolved as Article, but CurrentPage was still set to NewsHome page. After I assigned selected article as RoutedContentLink the page has started to work.

Value for URL segment

The articles page worked, but the I would like to have /news/test%20page%201/ URL to look more like this: /news/test-page-1/.

partial router encoded name in URL

I tried to change browser URL segment to “test-page-1” but it still didn’t work.

partial router wrong URL

This behavior can be changed in GetPartialVirtualPath. Instead of using “content.Name” we could use “content.UrlSegment” property. It is worth to remember that,  while searching article by URL segment we cannot use name property and we should use UrlSegment property as well.

After this change, “test-page-1” segment starts working.

partial router working example

The complete snippet: