Generating RSS and ATOM Feeds In WebMatrix

4.93 (15 votes)

I've previously looked at how to generate RSS feeds for both Web Forms and MVC using a variety of techniques, so it is only right that I look at a couple of ways to do this in Web Pages - the Razor based web development model supported by WebMatrix.

Just to recap, RSS, or Really Simple Syndication is an XML-based way to share or distribute content. ATOM was designed to overcome some of the perceived issues that afflicted RSS, and enjoys some support around the place too. Either way, most RSS readers accept ATOM feeds and vice-versa, so if you are asked to produce either ATOM or RSS, this article should help you.

I am using my Books database for the examples. Ideally your data will have a date created or similar against it. The sample database only has a date that the book was published, so it's not perfect but should serve for the purposes of illustration. The first example looks at generating RSS from LINQ to XML. The second and third look at producing both RSS and ATOM from a third party library - Argotic.

LINQ TO XML

LINQ to XML is a nice feature which was added in C# 3. It makes programming against XML more enjoyable than previous APIs. The following code is straightforward, and even mirrors the structure of the resulting XML to an extent:

@using System.Xml.Linq;
@{
    Layout = null;
    var db = Database.Open("Books");
    var url = "/Book/{0}";
    var sql = "Select BookId, Title, Description, DatePublished From Books";
    var items = db.Query(sql);
    var rss = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
        new XElement("rss",
          new XAttribute("version", "2.0"),
            new XElement("channel",
              new XElement("title", "Books News Feed"),
              new XElement("link", Functions.GetSiteUrl("~/rss")),
              new XElement("description", "Latest additions to Books"),
              new XElement("copyright", "(c)" + DateTime.Now.Year),
                items.Select(item => 
                    new XElement("item",
                    new XElement("title", item.Title),
                    new XElement("description", item.Description),
                    new XElement("link", Functions.GetSiteUrl(String.Format(url, item.BookId))),
                    new XElement("pubDate", item.DatePublished.ToString("R"))
                   ))
              )
        )
      );
    Response.ContentType = "text/xml";
    rss.Save(Response.OutputStream);
}

Some points of note: The first thing I did in this file - after referencing System.Xml.Linq - was to set the Layout page to null. This page should never have a layout page associated with it. Its job is to render pure XML. Having a layout page applied will cause the XML to become corrupted by HTML being injected into the output. You might think "but I know that - I just won't set a layout page for this file!" and indeed this will probably suffice most of the time. However, all it takes is for you, or someone else at some stage in the future to set the layout page in PageStart, for it to affect every file in the same or child folders, and Bang! your feed is broken. For this reason, you should get into the habit of declaring Layout = null at the top of every file like this.

The second point of note is the GetSiteUrl() function. I borrowed this from pranavkm, who I believe works for Microsoft. It's a nice little utility method that constructs the URL for a link based on the current environment. That means that the link will be generated from http://localhost:xxxxx during testing, but the full domain in production. No messing about from one deployment to another. The code for the function is this:

@functions {
  public static Uri GetSiteUrl(string url = "~/") {
    url = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute(url);
    return new Uri(url);
  }
}

I hadn't really looked at some of the obscurer methods that belong to Uri's before this. I might well have to explore them in a future article. In the meantime, back to the RSS feed. Once the XDocument has been constructed in memory, it is serialised to the Response output stream, with the correct content type having been set for the Response.

Argotic RSS

The Argotic Syndication Framework is a comprehensive library available both via Codeplex and NuGet. It includes a lot of tools for generating syndicated content or for consuming it, and seem to be pretty popular. Since it is available via NuGet, it is also available from the WebMatrix Package Manager. When you log into the Web Pages Administration tool, make sure you choose Default (All) as your packages source to access the whole of NuGet. From that point, you can simply search for "Argotic" and choose to install the Core library. Two other dependencies are also intalled - Argotic Common and Argotic Extensions. Once you have that installed, you can start working with the library:

@using Argotic.Syndication
@{
    Layout = null;
    var db = Database.Open("Books");
    var url = "/Book/{0}";
    var sql = "Select BookId, Title, Description, DatePublished From Books";
    var books = db.Query(sql);

    var feed = new RssFeed 
                   { 
                       Channel.Link = Functions.GetSiteUrl("~/argotic_rss"),
                       Channel.Title = "Books News Feed",
                       Channel.Description = "Latest additions to Books"
                   };

    foreach (var book in books)
    {
        var item = new RssItem
                       {
                           Title = book.Title,
                           Link = Functions.GetSiteUrl(String.Format(url, book.BookId)),
                           Description = book.Description,
                           PublicationDate = book.DatePublished
                       };
        feed.Channel.AddItem(item);
    }

    Response.ContentType = "text/xml";
    feed.Save(Response.OutputStream);
}

Argotic offers three types of Feed object - RssFeed, AtomFeed and GenericSyndicationFeed. We are only interested in the RssFeed object for this exercise. The library is really simple to use, and actually uses less code than the LINQ to XML version. It also offers the benefit of generating valid XML for you, so you don't have to worry about typos in your LINQ to XML XElement names. It's really difficult to discuss the code in any detail, as it is so self-explanatory. This is probably one of the best indicators of a very well designed API.

Argotic ATOM

Now that you have the Argotic library available to you, you can try the ATOM version:

@using Argotic.Syndication
@{
    Layout = null;
    var db = Database.Open("Books");
    var url = "/Book/{0}";
    var sql = "Select BookId, Title, Description, DatePublished From Books";
    var books = db.Query(sql);

    var feed = new AtomFeed
                   {
                       Id = new AtomId(Functions.GetSiteUrl()),
                       Title = new AtomTextConstruct("Books News Feed"),
                       UpdatedOn = DateTime.Now,
                   };

    feed.Authors.Add(new AtomPersonConstruct("Mikesdotnetting"));

    var selfLink = new AtomLink
                       {
                           Relation = "self", 
                           Uri = Functions.GetSiteUrl("~/argotic_atom")
                       };
    feed.Links.Add(selfLink);
    foreach (var book in books)
    {
        var entry = new AtomEntry
                        {
                            Id = new AtomId(Functions.GetSiteUrl(String.Format(url, book.BookId))),
                            Title = new AtomTextConstruct(book.Title),
                            UpdatedOn = book.DatePublished,
                            Summary = new AtomTextConstruct(book.Description)
                        };

        var alternateLink = new AtomLink
                                {
                                    Relation = "alternate",
                                    Uri = Functions.GetSiteUrl(String.Format(url, book.BookId))
                                };
        entry.Links.Add(alternateLink);
        feed.AddEntry(entry);
    }
    Response.ContentType = "application/atom+xml";
    feed.Save(Response.OutputStream);
}

In principal there are not too many differences between Argotic ATOM and RSS. One is constructed out of RssItem objects, while the other is a collection of AtomEntry objects. Interestingly, "application/atom+xml" is an official MIME type, whereas "application/rss+xml" is not. Choices for MIME Types for RSS are "application/xml" or "text/xml".

 

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 19804

3 Comments

- hr

Mike - I am running into the following error using the GetSitURL function:

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.WebPages.WebPageRenderingBase.Request.get'

Source Error:
Line 4: public static Uri GetSiteUrl(string url = "~/")
Line 5: {
Line 6: url = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute(url);
Line 7: return new Uri(url);
Line 8: }

- Mike

@hr,

I didn't make it clear in the article but you should put that in a file called Functions.cshtml and place it in a folder called App_Code.

- Manos

Hello,
I try to do the same as you without success. Could you provide more in detail explanation about your code especailly when you refer to var url = "/Book/{0}";
and also GetSiteUrl("~/rss")), or GetSiteUrl("~/argotic_rss"), (do I have to create those, they will generate automatically)because I cannot understand what to do.
If you have this article with the code and the database to be download it will help me a lot
Thanks in advance, Manos

Recent Comments

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using asp.net web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...

Harris Boyce 04/07/2017 04:17
In response to Razor Pages - The Elevator Pitch
As a developer of a couple "trivial" web pages applications used by non-profits that wouldn't have I...

Cyrus 28/06/2017 20:25
In response to Razor Pages - Getting Started With The Preview
.net core 2.0 preview 2: <a...

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...