Artem Community
Artem interactive communities
ASP.NET MVC View - More than just a page view. Part I

Generate RSS View

Introduction

Recently I was rebuilding my web site using ASP.NET MVC.
I really need to finish it in some stage, and then I decided to give a try to the ASP.NET MVC.
During my work I discovered the views are much more than just a page view and actually I found some fancy usages of ASP.NET MVC views.
In short series of articles I will share some of my fancy usages of ASP.NET MVC View.

Background

When building an ASP.NET MVC web site I start using views for generating pages, same as I have used WebForms. Then at some point I realized ASP.NET MVC View is much more than a page view/markup.
Actually with ASP.NET MVC we as asp.net developers are not tailored any more to a particular markup where form with runat=server is required. Further more, We are even not tailored to HTML  as view markup any more.

Using the Code

Having an ASP.NET MVC web site I came to a task to generate some RSS feeds. First, as usual, I started implementing an HttpHandler for that. Then I realized, I can use a controller and views and my view markup could be the RSS XML markup.

Here the code of my RSS generating:

  1. Part of my RssController where I provide the feeds data.

    public class RssController : Controller {

     

            #region Methods ///////////////////////////////////////////////////////////////////////////

     

            /// <summary>

            /// Indexes this instance.

            /// </summary>

            /// <returns></returns>

            public ActionResult Index() {

     

                RssFeed feed = new RssFeed();

                feed.Title = "Velio's Products";

                feed.Description = "The list of all my products.";

                feed.Link = HttpContext.IsDebuggingEnabled ? "/" : "http://artembg.com/";

     

                bool flag = false;

                foreach (Product product in Product.GetAll()) {

                    if (!flag) {

                        feed.PubDate = feed.LastBuildDate = product.ReleaseDate;

                        flag = true;

                    }

                    feed.Items.Add(new RssFeedItem {

                        Title = product.Title,

                        Description = product.Description,

                        Link = product.HomeUrl,

                        Guid = product.HomeUrl,

                        PubDate = product.ReleaseDate

                    });

                }

     

                return View(feed);

            }

  2. The Index.aspx view. Take a note there is no space between the page definition (<%@ Page ... %>) and the XML markup.

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Artem.WebSite.Models.RssFeed>" %><?xml version="1.0" encoding="utf-8" ?>

    <rss version="2.0">

        <channel>

            <title><%= Model.Title %></title>

            <link><%= Model.Link %></link>       

            <ttl>10</ttl>

            <pubDate><%= Model.PubDateString %></pubDate>

            <lastBuildDate><%= Model.LastBuildDateString %></lastBuildDate>

            <description><![CDATA[

            <%= Model.Description %>

            ]]></description>

            <% foreach (var item in Model.Items) { %>

            <item>

                <title><%= item.Title %></title>

                <link><%= item.Link %></link>

                <guid><%= item.Link %></guid>

                <pubDate><%= item.PubDateString %></pubDate>

                <description><![CDATA[

                <%= item.Description %>

                ]]></description>

            </item>

            <% } %>

        </channel>

    </rss>

  3. The entire RssFeed class:

    /// <summary>

    ///

    /// </summary>

    public class RssFeed {

     

        #region Static Fields /////////////////////////////////////////////////////////////////////

     

        List<RssFeedItem> _items;

     

        #endregion

     

        #region Properties  ///////////////////////////////////////////////////////////////////////

     

        public string Title { get; set; }

     

        public string Description { get; set; }

     

        public string Link { get; set; }

     

        public DateTime PubDate { get; set; }

     

        public DateTime LastBuildDate { get; set; }

     

        public string PubDateString {

            get {

                return PubDate.ToUniversalTime().ToString("R");

            }

        }

     

        public string LastBuildDateString {

            get {

                return LastBuildDate.ToUniversalTime().ToString("R");

            }

        }

     

        public List<RssFeedItem> Items {

            get {

                return _items ?? (_items = new List<RssFeedItem>());

            }

        }

        #endregion

    }

  4. The entire RssFeedItem class:

    /// <summary>

    ///

    /// </summary>

    public class RssFeedItem {

     

        #region Properties  ///////////////////////////////////////////////////////////////////////

     

        public string Title { get; set; }

     

        public string Description { get; set; }

     

        public string Link { get; set; }

     

        public string Guid { get; set; }

     

        public DateTime PubDate { get; set; }

     

        public string PubDateString {

            get {

                return PubDate.ToUniversalTime().ToString("R");

            }

        }

     

        #endregion

    }

Points of Interest

And because "Video worth a Million Words", take a look at my short video on this article here.

Once realizing the freedom We, ASP.NET developers, got with ASP.NET MVC Views, there is no limit of the views usage.
Stay tuned to the next part of this my short series about the ASP.NET Views.

Happy coding ...


Posted 03-10-2009 20:38 by velio

Comments

gonace wrote re: ASP.NET MVC View - More than just a page view. Part I
on 05-16-2009 16:47

Can't get this to work. I've getting an load error when loading ViewPage, RssFeedItem

Could not load type 'System.Web.Mvc.ViewPage<gonace.Helpers.Rss.RssFeedItem>'.

But the namespace and so on is correct :/

Powered by Community Server (Non-Commercial Edition), by Telligent Systems