Click here to Skip to main content
15,881,715 members
Articles / Web Development / XHTML

Consuming RSS Feed in MVC ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Aug 2009CPOL 30.1K   15   1
In this post, I will build an MVC application that will consume an RSS feed.

Introduction

In this article, I will build an MVC application that will consume an RSS feed. I will modify my previous project for RSS Feed. Follow these steps.

Select the "Controllers" Folder and then right click and click on Controller as shown below:

Press Add, and RssFeedController class will be created as shown below. The SyndicationFeed class from System.ServiceModel.Syndicatation makes it easy to work with RSS Feed. The code below uses an RSS feed from weblogs.asp.net to display ASP.NET weblogs on a page.

C#
using System.ServiceModel.Syndication;        // add a System.ServiceModel.Web.dll reference 
public class RSSFeedController : Controller
{
        public ActionResult RSSFeed() 
       {
          string strFeed = "http://weblogs.asp.net/aspnet-team/rss.aspx"; 
          using (XmlReader reader = XmlReader.Create(strFeed)) 
         { 
                  SyndicationFeed rssData = SyndicationFeed.Load(reader); 
                 return View(rssData); 
         }
     }
}

Repeat the above step 2, and Add RSSFeed view in Views\RSSFeed folder. Add the following code in RSSFeed view.

ASP.NET
<%@ Page Title="" Language="C#" 
MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<SyndicationFeed>" %> 
<%@ Import Namespace="System.ServiceModel.Syndication"%>
<asp:Content ID="Content1" 
ContentPlaceHolderID="TitleContent" runat="server">
RSSFeed
</asp:Content> 
<asp:Content ID="Content2" 
ContentPlaceHolderID="MainContent" runat="server">
<h2>RSSFeed</h2>
<% foreach (var item in ViewData.Model.Items) 
{ 
string URL = item.Links[0].Uri.OriginalString; 
string Title = item.Title.Text; 
Response.Write(string.Format("<p>
<a href=\"{0}\"><b>{1}</b></a>", URL, Title)); 
Response.Write("<br/>" + item.Summary.Text + "</p>"); 
} %>
</asp:Content>

Now you can run the project and it will display RSS feed from weblogs.asp.net as shown below:

Summary

In this article, we built an RSS Feed application using System.ServiceModel.Syndicatation namespace.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRSS Feed Pin
DBNelson11-Aug-11 5:27
DBNelson11-Aug-11 5:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.