
Using Alternate Templates in URLs after Upgrading Umbraco

It used to be in old versions of Umbraco - and I do mean oooold, like v7 - you could use alternate templates as part of the URL structure. This was particularly convenient when you wanted to have a "virtual node" of a sort for a page, but you didn't want to have it somewhere in the backoffice.
A great example of this is an RSS feed for a blog. In v7, you could assign an "Rss.cshtml" template to your article listing node and have it not be the default template. Then with no extra effort, you could have a URL such as https://mysite.com/blog/rss/ and it would just yoink the allowed template with the name "Rss" and apply it to the blog page but as a virtual "sub node".
At first glance, it looks like this no longer exists in Umbraco v9+ but that's not actually true. You can still do this even in version 15 - it just takes a slight bit of extra set-up to get going.
First off, you need to apply a content finder to make this happen. You don't need to make a custom one, you just need to apply it, and for that I recommend having a custom UmbracoBuilderExtensions.cs
file. My bare minimum version would look something like this:
namespace AltTemplateTest.Extensions
{
public static class UmbracoBuilderExtensions
{
public static IUmbracoBuilder AddAltTemplateSite(this IUmbracoBuilder builder)
{
// We are going to add our custom website configuration here.
return builder;
}
}
}
And then we need to register it in our Program.cs
file.
using AltTemplateTest.Extensions;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.CreateUmbracoBuilder()
.AddBackOffice()
.AddWebsite()
.AddComposers()
.AddAltTemplateSite() // This is where we add our custom website configuration.
.Build();
WebApplication app = builder.Build();
await app.BootUmbracoAsync();
Note: If you've read my article for 24 Days or any of my articles for Skrift, you probably see this is a common pattern I use for registering services, content finders, and other custom code for my site. I really like to break this up with additional methods that I use to group things to make finding where I've registered them easier, but we're going to keep it simpler for this example.
Now the magic! Back in our UmbracoBuilderExtensions
, we need to add one line where I conveniently left that comment, like so:
public static IUmbracoBuilder AddAltTemplateSite(this IUmbracoBuilder builder)
{
builder.ContentFinders().Append<ContentFinderByUrlAndTemplate>();
return builder;
}
Once you've added that, Umbraco will now find any of those alt templates you want to use and allow them to be used in the URL as if they were an extension of the page - perfect for a custom RSS feed or other secret little easter egg you want to include on your site!