Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building an RSS Feed in ASP.NET MVC

DZone's Guide to

Building an RSS Feed in ASP.NET MVC

·
Free Resource
When building a website it is common to expose an RSS/ATOM feed for your content. Feeds serve two main purposes. The first, is that it allows other sites to consume your content for syndication. For example if you write .NET articles, there may be other partner sites that subscribe to your feed and dynamically pull in your content.  Secondly, feeds allow users to subscribe to your site so they can get notifications when your content is updated. This is especially relevant for sites with irregular content updates such as a personal blog.

In 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.

 

Topics:

Published at DZone with permission of Michael Ceranski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}