In this tutorial, you will learn how you can create an RSS feed that is based on the content that has been created within a Umbraco v9 powered website. Exposing your site content via an RSS feed can be a good way to get extra eyeballs on your website and increase content loyalty with your customers. If you want to build an EPIC RSS feed, this is the tutorial for you 🔥🔥🔥
How To Create An RSS Feed In ASP.NET 5
Instead of trying to reinvent the wheel, we want to make life as easy as possible by leveraging code and packages that already exist. The process for creating a feed within Umbraco is very similar to creating a feed within a normal .NET Core website. This is good news as it means that you will be able to use a number of vanilla .NET tools to create a feed.
The first tool you will need to install is called System.ServiceModel.Syndication.
ServiceModel.Syndication has been around for a long time, and can be installed via NuGet:
ServiceModel.Syndication comes with lots of useful helpers and utility methods that make it really easy to build the data structure required to power an RSS feed 😊
How To Build An RSS Feed Within Umbraco
In order to build an RSS feed within Umbraco, you will need to create a few key components. Below gives a breakdown of each file:
Controller: With the prerequisites installed, the first thing that you will need to create is a controller. The way I like to build my feed is to use be a vanilla MVC
If you saw my previous post (A Guide To Umbraco V9 Content Modeling, I discussed the benefit of creating simple controllers that used service to pull in the data. In this example, I do all the feed creation within a service called
IRssBuilderService. In order to create your own custom service, you will need to register it with the dependency injection framework. This can be done in a
Composer using this code:
Service: The next class will be used to generate the data that will be exposed by the feed. Be warned! This process is quite long as we need to set a lot of data, so hang on in 😊
On-Line 13 you will notice the use of
IBlogService. This is another great example of why I favour using service patterns. As I already built the
IBlogService to render blogs on my homepage. Accessing the blog posts added within the CMS was easy as the code already existed in my starter kit. I will not cover the code for the blog server here, however, you can find it all here.
Composer: If you want to work with a normal
Controller within ASP.NET Core, you will need to add a routing rule in order for a request to be allowed to reach it, you do this within
Startup. The code looks like this:
This extension can be be registered within
Startup like this:
Validation: When creating and generating an RSS feed, you can not just knock out any old XML. Your RSS feed will need to adhere to the RSS specification. Failing to return all the required data, will mean your feed may not display correctly in certain readers, meaning your subscribers will not see your updates 😞. When building your feed, I recommend you test it frequently against the W3C Feed Validation Service. Creating a valid feed can be very tricky and take you a while to get correct. The code above will return out all the required properties, however, testing if your friend 💥
You are now an absolute RSS creation machine 🤖. Building a feed-in Umbraco is very similar to creating one in any >NET Core website. The main difference within Umbraco is that you need to register your controller in the routeing table and you need to use the Umbraco API in order to get the data from the CMS. After that, the process should feel very familiar. Happy Coding 🤘