If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Consuming RSS Feed in MVC ASP.NET

by Farooq Kaiser 8. July 2009 04:29

In last article, we explored the ASP.net MVC application structure. Today, we will build a MVC application that will consume an RSS feed. I will modify my previous project for RSS Feed. Follow the these steps.

  1. Select the "Controllers" Folder and then right click and click on Controller as shown below.
  2. 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 = "http://weblogs.asp.net/aspnet-team/rss.aspx"; 	
    	
    		          using (XmlReader reader = XmlReader.Create(strFeed)) 	
    	
    		         { 	
    	
    		                  SyndicationFeed rssData = SyndicationFeed.Load(reader); 	
    	
    		                 return View(rssData); 	
    	
    		         }	
    	
    		     }	
    	
    		}	
    	

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

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

Summary 

In this article, we build an RSS Feed application using System.ServiceModel.Syndicatation namespace. In, next article we will explore form validation with MVC ASP.net

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.NET | asp.net | C#

Comments

Jobs Autos Real estate Videos Power by Google