Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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</p> <pre><code>using (XmlReader reader = XmlReader.Create(feed)) { SyndicationFeed rssData = SyndicationFeed.Load(reader); return View(rssData); } </code></pre> <p>Below is the controller and UserControl code:</p> <p>The Controller code is:</p> <pre><code>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; } } } </code></pre> <p>The UserControl</p> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Index.ascx.cs" Inherits="MvcWidgets.Views.RssWidget.Index" %&gt; &lt;div class="RssFeedTitle"&gt;&lt;%= Html.Encode(ViewData.Model.Title.Text) %&gt; &amp;nbsp; &lt;%= Html.Encode(ViewData.Model.LastUpdatedTime.ToString("MMM dd, yyyy hh:mm:ss") )%&gt;&lt;/div&gt; &lt;div class='RssContent'&gt; &lt;% foreach (var item in ViewData.Model.Items) { string url = item.Links[0].Uri.OriginalString; %&gt; &lt;p&gt;&lt;a href='&lt;%= url %&gt;'&gt;&lt;b&gt; &lt;%= item.Title.Text%&gt;&lt;/b&gt;&lt;/a&gt; &lt;% if (item.Summary != null) {%&gt; &lt;br/&gt; &lt;%= item.Summary.Text %&gt; &lt;% } } %&gt; &lt;/p&gt; &lt;/div&gt; </code></pre> <p>with the code behind modified to have a typed Model</p> <pre><code>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&lt;SyndicationFeed&gt; { } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload