views:

1443

answers:

3

I'm new to the MVC framework and wondering how to pass the RSS data from the controller to a view. I know there is a need to convert to an IEnumerable list of some sort. I have seen some examples of creating an anonymous type but can not figure out how to convert an RSS feed to a generic list and pass it to the view.

I don't want it to be strongly typed either as there will be multiple calls to various RSS feeds.

Any suggestions.

A: 

A rss is a xml file with special format. You may design a dataset with that generic format and read the rss(xml) with ReadXml method and the uri as the path to the file. Then you have got a dataset you can consume from another clases.

Javier Suero Santos
+4  A: 

I've been playing around with a way of doing WebParts in MVC which are basically UserControls wrapped in a webPart container. One of my test UserControls is an Rss Feed control. I use the RenderAction HtmlHelper extension in the Futures dll to display it so a controller action is called. I use the SyndicationFeed class to do most of the work

using (XmlReader reader = XmlReader.Create(feed))
{
    SyndicationFeed rssData = SyndicationFeed.Load(reader);

    return View(rssData);
 }

Below is the controller and UserControl code:

The Controller code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Security;
using System.IO;

namespace MvcWidgets.Controllers
{
    public class RssWidgetController : Controller
    {
        public ActionResult Index(string feed)
        {
            string errorString = "";

            try
            {
                if (String.IsNullOrEmpty(feed))
                {
                    throw new ArgumentNullException("feed");
                }
                    **using (XmlReader reader = XmlReader.Create(feed))
                    {
                        SyndicationFeed rssData = SyndicationFeed.Load(reader);

                        return View(rssData);
                    }**
            }
            catch (ArgumentNullException)
            {
                errorString = "No url for Rss feed specified.";
            }
            catch (SecurityException)
            {
                errorString = "You do not have permission to access the specified Rss feed.";
            }
            catch (FileNotFoundException)
            {
                errorString = "The Rss feed was not found.";
            }
            catch (UriFormatException)
            {
                errorString = "The Rss feed specified was not a valid URI.";
            }
            catch (Exception)
            {
                errorString = "An error occured accessing the RSS feed.";
            }

            var errorResult = new ContentResult();
            errorResult.Content = errorString;
            return errorResult;

        }
    }
}

The UserControl

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Index.ascx.cs" Inherits="MvcWidgets.Views.RssWidget.Index" %>
<div class="RssFeedTitle"><%= Html.Encode(ViewData.Model.Title.Text) %> &nbsp; <%= Html.Encode(ViewData.Model.LastUpdatedTime.ToString("MMM dd, yyyy hh:mm:ss") )%></div>

<div class='RssContent'>
<% foreach (var item in ViewData.Model.Items)
   {
       string url = item.Links[0].Uri.OriginalString;
       %>
   <p><a href='<%=  url %>'><b> <%= item.Title.Text%></b></a>
   <%  if (item.Summary != null)
       {%>
        <br/> <%= item.Summary.Text %>
    <% }
   } %> </p>
</div>

with the code behind modified to have a typed Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;

namespace MvcWidgets.Views.RssWidget
{
    public partial class Index : System.Web.Mvc.ViewUserControl<SyndicationFeed>
    {
    }
}
Matthew
Nice, you have given me a great example to work with.
TriFatlete
I'm glad it helped. Did your application go well?
Matthew
After a few months, I finally got to putting this together. It did work perfectly. Thanks again.
TriFatlete
+4  A: 

@Matthew - perfect solution - as an alternative to code behind which tends to break the MVC concept, you can use:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SyndicationFeed>" %>  
<%@ Import Namespace="System.ServiceModel.Syndication" %>
viperguynaz