02 June 2014 HowTo, RSS CI Team

I’ve already written about how to consume RSS or Atom Feeds with the SyndicationFeed (in German, sry) but todays post is about how to create or publish an own feed. Beside the age of the feed standard and the much defamation from Facebook, Twitter and Google on RSS/Atom I consider this easy to consume API as quite handy.

Alright… ASP.NET MVC Infrastructure code: RSS result

The inspiration of the ActionResults came from this blog. The implementation is trivial. All you might need is the reference on System.ServiceModel.

   1: public class RssResult : FileResult

   2: {

   3:     private readonly SyndicationFeed _feed;

   4:  

   5:     /// <summary>

   6:     /// Creates a new instance of RssResult

   7:     /// based on this sample 

   8:     /// http://www.developerzen.com/2009/01/11/aspnet-mvc-rss-feed-action-result/

   9:     /// </summary>

  10:     /// <param name="feed">The feed to return the user.</param>

  11:     public RssResult(SyndicationFeed feed)

  12:         : base("application/rss+xml")

  13:     {

  14:         _feed = feed;

  15:     }

  16:  

  17:     /// <summary>

  18:     /// Creates a new instance of RssResult

  19:     /// </summary>

  20:     /// <param name="title">The title for the feed.</param>

  21:     /// <param name="feedItems">The items of the feed.</param>

  22:     public RssResult(string title, List<SyndicationItem> feedItems)

  23:         : base("application/rss+xml")

  24:     {

  25:         _feed = new SyndicationFeed(title, title, HttpContext.Current.Request.Url) { Items = feedItems };

  26:     }

  27:  

  28:     protected override void WriteFile(HttpResponseBase response)

  29:     {

  30:         using (XmlWriter writer = XmlWriter.Create(response.OutputStream))

  31:         {

  32:             _feed.GetRss20Formatter().WriteTo(writer);

  33:         }

  34:     }

  35: } 

The Action

With the ActionResult we are able to return the controller a feed.

   1: public virtual ActionResult Feed(string id)

   2:     {

   3:         var items = new List<SyndicationItem>();

   4:  

   5:         for (int i = 0; i < 10; i++)

   6:         {

   7:             string feedTitle = "Test Title " + i;

   8:  

   9:             var helper = new UrlHelper(this.Request.RequestContext);

  10:             var url = helper.Action("Index", "Home", new { }, Request.IsSecureConnection ? "https" : "http");

  11:  

  12:             var feedPackageItem = new SyndicationItem(feedTitle, "Test Description " + i, new Uri(url));

  13:             feedPackageItem.PublishDate = DateTime.Now;

  14:             items.Add(feedPackageItem);

  15:         }

  16:  

  17:         return new RssResult("Demo Feed", items);

  18:     }   

The only trap is the URL. Usually the UrlHelper creates only relative links. But with the declaration of all parameters it turns into a proper URL.

Result

image

Works.

If your are looking for the complete example GitHub will be a help like always.