Click here to Skip to main content
Click here to Skip to main content

Consuming RSS Feed in MVC ASP.NET

By , 27 Aug 2009
 

Introduction 

In this article, i will build a MVC application that will consume an RSS feed. I will modify my previous project for RSS Feed. Follow the 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.

using System.ServiceModel.Syndication;        // add a System.ServiceModel.Web.dll reference 
public class RSSFeedController : Controller
{
        public ActionResult RSSFeed() 
       {
          string strFeed = "<a href="%22%22%22%22http://weblogs.asp.net/aspnet-team/rss.aspx%22%22%22%22">http://weblogs.asp.net/aspnet-team/rss.aspx</a>"; 
          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.
<%@ 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)

About the Author

Farooq Kaiser
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
Member
12+ years of complete software development life cycle experience for web based applications and multi-tier client-server desktop, primarily using LINQ, WCF, WWF, C#, ASP.NET, XML, XSLT, AJAX, Winforms,Visual Basic, JavaScript, JQuery, Google APIs, C++, VB.NET, C, ATL/COM, Open XML. Extensively involved in the requirement analysis, feasibility study, conceptualization, planning, architecture/design, configuration, development, quality assurance, implementation and release of the software products.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRSS FeedmemberDBNelson11 Aug '11 - 5:27 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 27 Aug 2009
Article Copyright 2009 by Farooq Kaiser
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid