DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Building an RSS Feed in ASP.NET MVC

Building an RSS Feed in ASP.NET MVC

Michael Ceranski user avatar by
Michael Ceranski
·
Apr. 13, 10 · News
Like (1)
Save
Tweet
Share
14.18K Views

Join the DZone community and get the full member experience.

Join For Free
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.

 

ASP.NET MVC ASP.NET

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Concurrency: LockSupport
  • Tackling the Top 5 Kubernetes Debugging Challenges
  • MongoDB Time Series Benchmark and Review
  • Best Practices for Setting up Monitoring Operations for Your AI Team

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: