Posts Tagged ‘MVC3’

I’m using the first Umbraco 5 (Jupiter) RTM on a new project.

One of the requirements of this project is to aggregate RSS feeds, so I built a simple plugin leveraging the System.Xml.Linq objects.

To make the plugin, I created a new MVC 3 project in visual studio to build and test the functionality.
I would have added the Umbraco test site to my solution and setup post build commands to copy the files from my project to Umbraco folders, but for some reason I was not able to run the Umbraco site from Visual Studio. I think its a system problem though.

I’ve also used the HTML Agility pack to parse the feed descriptions and remove bad markup.

Here is the  code to grab the RSS Feed and parse it with the HTMLAgilityPack –

    public static class Reader
         public static IEnumerable GetRssFeed(string feedUrl, bool trimDescription = true)
             XDocument feedXml = XDocument.Load(feedUrl);
             XNamespace dc = "";
             string exp = @"^.{1,500}";
             if (trimDescription == false)
                 exp = ".*";
             var feeds = from feed in feedXml.Descendants("item")
                         select new Rss
                             Title = feed.Element("title").Value,
                             Link = feed.Element("link").Value,
                             Description = Regex.Match(ParsedDoc(feed.Element("description").Value).DocumentNode.InnerText, exp).Value,
                             PubDate = feed.Element("pubDate").Value,
                             Creator = feed.Element(dc + "creator").Value
             return feeds;

         public static HtmlAgilityPack.HtmlDocument ParsedDoc(string source)
             HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
             doc.OptionWriteEmptyNodes = true;
             doc.OptionFixNestedTags = true;
             return doc;



And the RSS class…


    public class Rss : IComparable
        public string Link { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime PubDateDate { get; set; }
        public string Creator { get; set; }

        public string PubDate 
                return _pubDate;
                _pubDate = value;
                DateTime pubDateDate = DateTime.MinValue;
                DateTime.TryParse(value, out pubDateDate);
                PubDateDate = pubDateDate;
        private string _pubDate;

        // Implement the default CompareTo method with the PubDate
        // as the parameter. 
        public int CompareTo(Rss other)
            // If other is not a valid object reference, this instance is greater.
            if (other == null) return 1;
            return PubDateDate.CompareTo(other.PubDateDate);


In Umbraco, I created a folder in the plugins directory following the Dev Dataset examples.
I placed the HTMLAgilityPack DLL and XML, and my DLL in this folder to allow the CSHTML to call the classes.

I created 2 new document types in Umbraco for an ‘RSS List’ and ‘RSS Feed Item’. On the RSS List template, I grab all the children of that node (which are of type ‘RSS Feed Item’) and grab the properties from them using the DynamicModel.

In the Umbraco RSS List template…

@inherits Umbraco.Cms.Web.RenderViewPage

@functions {
    //Add public properties here to create Macro Parameters

    protected List<Zeroth.Umbraco.RSSReader.Rss> aggregatedFeed = new List<Zeroth.Umbraco.RSSReader.Rss>();

    foreach (var child in DynamicModel.Children)
        aggregatedFeed.AddRange(Zeroth.Umbraco.RSSReader.Reader.GetRssFeed(child.url, true));


<div class="rssList">
@foreach (var child in aggregatedFeed)
    <div class="rssItem">
        <div class="rssLink">
            <a href="@child.Link" target="_blank">@Html.Encode(child.Title)</a>
        <div class="rssByLine">
            <span class="rssPubDate">@child.PubDate.Replace("+0000", "")</span><span> | </span><span class="rssCreator">@child.Creator</span>
        <div class="rssDescription">
            @( new HtmlString(child.Description) )

One of the great MVC3 benefits is how easy it is to create helpers to inject HTML into your views.

After reading this post, I decided I would use Bart Czernicki’s example to create an MVC3 helper ‘cshtml razor engine style’.

You can place the following code in your App_Code folder (in a cshtml file).
I use a file called ‘Helpers’ and place multiple helpers in the same file. You could also create multiple files there to namespace your helpers if you have a bunch.

 @helper SilverLightHostControl(
            string silverlightXapFileLocation, // URI Location of the Silverlight XAP file
            string minimumRuntimeVersion = "4.0.50826.0",
            string objectContainerWidth = "100%",
            string objectContainerHeight = "100%",
            string onErrorJavaScriptFunctionName = "onSilverlightError",
            string divObjectTagId = "silverlightControlHost",
            string iFrameStyle = "visibility:hidden;height:0px;width:0px;border:0px"
    <div id="@divObjectTagId">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="@objectContainerWidth" height="@objectContainerHeight">
            <param name="source" value="@silverlightXapFileLocation"/>
            <param name="minRuntimeVersion" value="@minimumRuntimeVersion" />
            <param name="onError" value="@onErrorJavaScriptFunctionName" />
            <param name="background" value="white" />
            <param name="autoUpgrade" value="true" />
            <a href="" style="text-decoration:none">
            <img src="" alt="Get Microsoft Silverlight" style="border-style:none"/>

To use the helper in your view, call it like this –

For help integrating a Silverlight project in MVC, check this post out –