Building an RSS Feed in ASP.NET MVC
Join the DZone community and get the full member experience.
Join For FreeIn the .NET 3.5 framework there is a namespace titled System.ServiceModel.Syndication which very few people seem to know about. The classes contained in this library allows you to create and consume RSS feeds with minimal effort. I recently created a feed for my WeBlog application. I was astonished about how little time it took to implement an RSS Feed. Instead of weighing you down with all the details...I'll let the code do the talking:
public ActionResult Feed( int? page, int? pageSize ) {
var query = from x in Engine.Posts.FindPosts( page, pageSize )
where
x.PublishDate != null
orderby
x.PublishDate descending
select x;
List<PostModel> posts = query.ToList();
Uri site = new Uri(Engine.GetWebAppRoot());
SyndicationFeed feed = new SyndicationFeed(Engine.Settings.SiteName, Engine.Settings.SiteDescription, site );
List<SyndicationItem> items = new List<SyndicationItem>();
foreach( PostModel post in posts ) {
SyndicationItem item = new SyndicationItem(post.Title, post.Content,
new Uri( Engine.GetWebAppRoot() + "/Posts/" + post.Slug), post.ID.ToString(), post.PublishDate ?? DateTime.Now);
items.Add(item);
}
feed.Items = items;
return new Core.RSSActionResult() { Feed = feed };
}
To implement this code in your MVC app, you will first need to add a reference to the System.ServiceModel assembly. Next, define an action in one of your controllers to support the Feed. In My case, I added a Feed action to the Home controller because it made the most logical sense. Once your controller action is defined its a simple task of pulling some data from your site and serializing it as XML using the Syndication classes. In the code above, I first pull back a list of posts from my data store using LINQ. Then I create a new SyndicationFeed object passing it a few parameters such as the Site Name, Description and the URI. Next a list of Syndication Items is built and assigned to the SyndicationFeed instance. Finally, I return a RSSActionResult. The RSSAction result class is just a method that I used to set the response content type as shown below:
public class RSSActionResult : ActionResult {
public SyndicationFeed Feed { get; set; }
public override void ExecuteResult(ControllerContext context) {
context.HttpContext.Response.ContentType = "application/rss+xml";
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output)) {
rssFormatter.WriteTo(writer);
}
}
}
By the way, you could set the content type and serialize the response output directly in the controller method. I just split it for clarity and re-usablity purposes.
Finally, if you are implementing a feed on your blog, I would recommend using feedburner as a "feed proxy". The reason I refer to feedburner as a "proxy" is because the feedburner URL will always stay the same. I can have a feed url named http://feeds.feedburner.com/codecapers and have it point to any underlying URL I want. Therefore, If I move my feed from "/Home/Feed" to "/Site/Feed", I can simply reconfigure my feedburner feed to point to the correct URL. Since subscriptions are pointed exclusively to the feedburner URL there will be no impact to my visitors. In addition, feedburner will also give you extra features, such as email subscriptions, twitter integration and analytics that would take a fair amount of time and effort to implement on your own.
Published at DZone with permission of Michael Ceranski, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments