In this guide, you will learn what partial routing is within Episerver CMS and why you may want to use it. The concept of partial routing is pretty easy to grasp, however, the implementation details took me a bit longer to get my head around. If you simply want to learn how to implement a partial router and you want access to some code, this is not the article for you. For those readers, check out this instead. This guide is pure theory so be warned. Want to learn would you want to use one, read on 🔥🔥🔥
What Is Partial Routing?
Episerver CMS allows content editors to create a website using a hierarchy of different content pages within a website. This is done by adding pages within the CMS content tree. Most content that appears on the website is directly mapped to the area within the content tree where it gets created. For example, under the home page, you might have some landing pages. Each of those pages might contain their own children. This page structure hierarchy, will dictate the Url hierarchy of your website.
www.website.com/listings/content will relate to two child pages of the homepage, one directly under the home page called
Listing and a page that lives under
Listings that is called
Content. Using a tree structure like this make it very easy for content editors to structure a website, however, in certain circumstances, it can cause us a few issues as it also creates a connection between your website's navigation structure and the editors content tree. Sometimes you might want these things to be different. Let's cover some scenarios when this might happen:
Example Of Partial Routers
Too Many Pages: I've talked previously about How To Deal With Large Amounts Of PageTypes. For example, you have a parent node with 1000 children. To create a good backend experience, you don't want to have all these pages under a single parent as it will affect performance. It will definitely make the content editors lives a nightmare when they update information. One pattern to avoid this situation is to use the container page pattern. Creating container pages will help to keep the content tree organized. You may decide to use container pages to organise the tree by date, like the year or month. Using container pages is great within the CMS, however, they can be pointless in the fronted website. The containers will affect the Url and potentially direct a site visitor to an empty or meaningless page.
In most circumstances, you won't want to display the container pages within your Url. A better approach would be to create a folder that lived outside of your website hierarchy and then implement some sort of solution so the Url for each blog post still looks like they all live under the main blog section
Taxonomy: If you have ever used a blog then you will be familiar with tags. Tags provide a useful way to group related posts/articles together, for example, this page tags may be 'Routing' and 'Episerver 7'. On this website, when you click on a 'tag' you will be redirected to a listings page that displays all articles related to that tag. A page might have multiple tags and may need to have multiple URLs in order for it to be accessed. This can be useful for long-tail URL generation Episerver, content can only live in one area in the page tree so we need to make the content look like it lives in multiple places within the site (this is terrible for SEO and not recommended).
Content That Doesn't Live In Episerver: In How To Display Non Episerver Content Within The Episerver Editor I talk about Episervers ability to render content that does not live in the Episerver database. You might have content that you want to display from a service or even an API. Just because the content doesn't live in Episerver, you still might want to create a Url on your website that maps to that page. Again, we need a way to manage this.
To deal with these situations in Episerver you can use a special type of routing provider that Episerver provides called the Partial Router. When you use a partial router, when your website first loads, you can tell Episerver that for any requests of a certain URL, use a custom routing solution that you define in code. Say you have a blog homepage called blog. You could tell Episerver that any request for blog (or any of its child pages) to use a custom router. In your custom router, you can then get the remaining segments in the Url and then reroute them to something different. In the example below that would be 'blog-post-1':
After you have this identified, you can then use whatever code you want to get the pages data, whether that be in the Episerver database or somewhere else. After you get your data you can pass it back into the ASP.NET request pipeline and then trigger a controller to render the UI. Using this approach means you can return pages/views for URLs that don't exist in the page tree. This allows you a lot of power to do some pretty cool things, like displaying a web page for content that doesn't even live in Episerver!!! Compare this to the old webform days and it's a massive improvement from several years ago.
In today's tutorial, we've covered the basics of why you may need a partial router on your site. When I first started looking into this topic, I struggled to get my head around this, so hopefully, this dummy's guide should give you an outline of the type of scenarios the partial router will solve for you. This is the first of a two-part series about partial routing. In the next article, I'll cover step-by-step how to create a partial router in code within Episerver. All the above code can be downloaded in a fully working website from my GitHub account here. Happy Coding 🤘