Today's guide will try to explain what partial routing is, why you may want to use it and how to implement using Episerver within your site. The concept of partial routing is pretty easy to grasp.. the implementation details took me a bit longer to get my head around. So before we go into the technical details, I'll try to present a few of the problems a partial router solves first.
What Is Partial Routing?
Episerver works by allowing content editors the ability to create a website using a hierarchy of different content types within a content tree. Most content that appears on the website gets displayed under the website page in the editor. Under the home page you might have some landing pages and under each one of those a few content pages. This Url hierarchy of your website will mirror this tree structure, so 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 are called content Using a tree structure like this makes 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.
Example One: Too Many Pages
I've talked previously about How To Deal With Large Amounts Of PageTypes. So, say you have a parent node with 1000 children. You don't want to have all these pages under a single page as it will affect performance and make the content editors lives a nightmare when they update information. If you decided to use a container page approach to help organize your website, say using a year/month folder structure, then the Url on the front end will also change to mirror this.
In most circumstances, you won't want to display the container pages within your Url. In these circumstances, putting content like this within your website hierarchy isn't ideal. A better approach would be to create a folder that lived outside of your website hierarchy and then implement some sort of solution to make the Url for each blog post to look like they all lived in the same place
Example Two: Taxonomy
If you're still struggling with the concept, then let's move onto the second example. 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 appear under multiple listing pages. In Episerver, content can only live in one area 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).
Example Three: Content that doesn't live in EPiServer
I've talked previously about How To Display Non Episerver Content Within The Episerver Editor and not all content displayed on your website might live in Episerver at all. You might have content that you want to display from a service or even a text file. Just because the content doesn't live in Episerver, you still might want to create a Url that maps to it. Again, we need a way to manage this.
Ok, so the hint is sort of in the title.. how we deal with these situations is via a special type of routing provider that Episerver gives us out the box 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 page, 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 part of the Url. 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 pipeline and let a relevant MVC controller match the request and display a view. Using this approach means you can return pages/views for URLs that don't exist in the page tree, which allows you a lot of power to do some pretty cool things, like display 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. In case you just want to jump right into the code, I have also included the fully working website that has the other article below:
All the above code can be downloaded in a fully working website from my GitHub account here. #ILoveEpiserver