Written on 8/8/2014 in Web development

Implementing RSS


ASP.NET MVC Writing RSS Feed

Well, this is one I'm genuinely afraid of. I don't mind reading on a bad looking website1 as long as the content's good and the font isn't too bad. Bad feeds, on the other hand, get removed a lot more easily! It's not that a feed is easy to 'get wrong'. A lot needs to happen at the core of the matter. But for every content based website, it has to be good. The reason I'm so afraid of implementing is that at the core, it seems sooo simple.. I mean, it's just some xml file on a url. But whenever something this simple is encountered, it's probably not true.

Invincible RSS [2]

Really Simple Syndication, however, is. When you follow the basic, core principles of RSS, your feed will look just fine for most readers. There's no need for any glitter and glamour here. The only problems I suspect to encounter are the ones I cause myself. I followed a blog post and implemented a custom action result that builds the url from a 'SyndicationFeed'. A class already present in the .Net framework. Using this action result, all I had to do was get my posts and fill up the SyndicationFeed. This syndicationfeed can also ease things up when I ever decide to move to Atom or another syndicationfeed.

Filling up the syndicationfeed:

SyndicationFeed feed = new SyndicationFeed("The Vorpal Feed",
                        "The Vorpal Blog is a blog that combines good reads with technical subjects. It's a journal about the journey in the magnificent world of a web developer.",
                        new Uri("http://www.thevorpal.be/Blog/Feed"),
                        "http://www.thevorpal.be/Blog/Feed",
                        DateTime.Now);
feed.Authors.Add(new SyndicationPerson { Name = "Koen T'Sas" });
feed.Categories.Add(new SyndicationCategory("Technology"));
feed.Categories.Add(new SyndicationCategory("Web development"));
feed.Language = "English";
feed.ImageUrl = new Uri("https://vorpalblogstorage.blob.core.windows.net/vorpalimages/Logo-v1-128.png");

List<SyndicationItem> items = new List<SyndicationItem>();
foreach (var post in allPosts)
{
    SyndicationItem item =
        new SyndicationItem(post.Title,
                            post.FormattedText,
                            new Uri("http://www.thevorpal.be/Blog/" + post.Topic.Slug + "/Post/" + post.Id),
                            "Post" + post.Id.ToString(),
                            DateTime.Now);
    item.Authors.Add(new SyndicationPerson { Name = "Koen T'Sas" });
    item.Categories.Add(new SyndicationCategory("Technology"));
    item.Categories.Add(new SyndicationCategory("Web development"));
    item.PublishDate = post.WrittenDate;
    item.Summary = new TextSyndicationContent(post.FormattedText);
    item.PublishDate = post.WrittenDate;

    items.Add(item);
}

feed.Items = items;
return new RssResult(feed);

The RSS action result:

public class RssResult : FileResult
{
    private readonly SyndicationFeed _feed;

    /// <summary>
    /// Creates a new instance of RssResult
    /// </summary>
    /// <param name="feed">The feed to return the user.</param>
    public RssResult(SyndicationFeed feed)
        : base("application/rss+xml")
    {
        _feed = feed;
    }

    /// <summary>
    /// Creates a new instance of RssResult
    /// </summary>
    /// <param name="title">The title for the feed.</param>
    /// <param name="feedItems">The items of the feed.</param>
    public RssResult(string title, List<SyndicationItem> feedItems)
        : base("application/rss+xml")
    {
        _feed = new SyndicationFeed(title, title, HttpContext.Current.Request.Url) { Items = feedItems };
    }

    protected override void WriteFile(HttpResponseBase response)
    {
        using (XmlWriter writer = XmlWriter.Create(response.OutputStream))
        {
            var formatter = _feed.GetRss20Formatter();
            formatter.SerializeExtensionsAsAtom = false;


            formatter.WriteTo(writer);
        }
    }
}

I then subscribed the public url that returns this RSS result on Feedburner, which will check from time to time3 whether there are new posts and makes it easier for people to subscribe. Using Feedburner I also get some nice statistics, and everyone LOVES statistics. The only main problem I found here was searching for help on the subject. RSS feeds are so SEO friendly that a lot of search queries 'about RSS' return 'RSS feeds about something else'. Anyhow, the feed's up, I just need a way to troubleshoot it now.

Troubleshooting

As it turns out, troubleshooting RSS feeds is a b*tch. Validating your RSS and getting it on feedburner isn't hard. But then I subscribed to my own RSS feed using feedly and an offline reader. The offline reader worked well, but wasn't updating the posts once they downloaded4. Feedly was acting strange, there were no updates on new posts although the feedburner feed was updating fine. There is obviously no way to see why these systems work or why they don't.

What I was looking for, was a free tool that shows me a shiny green message that tells me: "Hey, your feed is fine! Keep sharing, man.". But I did not find it. Since I do not have a way to recieve feedback on The Vorpal Blog right now, it's not easy to get it. But if you know a free tool that does just that, please share it with me on Twitter. The problems on Feedly were probably on their end. After an update, these were resolved and we all lived happily ever after. I merely lost a few hours of my life.

Read on, my friend, read on.

Newer Older Top
blog comments powered by Disqus