Naweed Akram's Blog
<sharing: Knowledge />
RSS Feed Result for ASP.NET MVC
Posted: August 18, 2010

If you notice to the right of this website, you will find a RSS icon, which when clicked, will expose all the posts on this site as RSS feed, which you can subscribe to in your favorite RSS news reader (or Google Reader for that matter). This task is much more simpler than anticipated. Lets dwell right into it and see how easy it is.


For the purpose of this post, let's create a new ASP.NET MVC 2.0 project. I am going to use a simple Model (as below) which will represent each blog post, news item or artical on your website. The bare minimum needed is implemented here.


    public class Post
        public int PostID { get; set; }

        public string PostTitle { get; set; }

        public string PostSummary { get; set; }

        public DateTime AddDate { get; set; }


Once we have the model, lets create an Action Result which will actually return the list of above posts in RSS 2.0 format.


    public class RssResult : ActionResult

        private List<Post> _items;

        public RssResult(List<Post> items)
            _items = items;

        public override void ExecuteResult(ControllerContext _context)
            XmlWriterSettings _xmlSettings = new XmlWriterSettings();

            _xmlSettings.Indent = true;
            _xmlSettings.NewLineHandling = NewLineHandling.Entitize;

            _context.HttpContext.Response.ContentType = "text/xml";

            using (XmlWriter _writer = XmlWriter.Create(_context.HttpContext.Response.OutputStream, _xmlSettings))
                //Write the RSS Header
                _writer.WriteAttributeString("version", "2.0");

                //Write the Channel Header

                //Title of the Blog
                _writer.WriteElementString("title", "Title of my Blog");
                //Description of the Blog
                _writer.WriteElementString("description", "Some description on my Blog");
                //URL of the Blog (in our case
                _writer.WriteElementString("link", _context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority));

                //Write each post item
                foreach (Post _post in _items)
                    //Write the Item Header
                    _writer.WriteElementString("title", _post.PostTitle);
                    _writer.WriteElementString("pubDate", _post.AddDate.ToString("ddd, dd MMM yyyy hh:mm:ss +0000"));
                    _writer.WriteElementString("description", "<div xmlns=\"\">" + _post.PostSummary + "</div>");
                    //Assuming that each post item can be viewed online at{PostID}
                    _writer.WriteElementString("link", _context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + "/Post/" + _post.PostID);

                    //End Item Header

                //End Channel Header
                //End RSS Header



Now, the last step left is to use the RSS Result. Let us create a new controller called RSSFeedController, which is as simple as this:


    public class RSSFeedController : Controller
        public ActionResult Index()
            List<Post> _items = new List<Post>();

            //Populate the Items
            _items.Add(new Post() { PostID = 1, PostTitle = "Title No. 1", PostSummary = "Summary No. 1", AddDate = new DateTime(2010, 2, 15) });
            _items.Add(new Post() { PostID = 2, PostTitle = "Title No. 2", PostSummary = "Summary No. 2", AddDate = new DateTime(2010, 2, 16) });
            _items.Add(new Post() { PostID = 3, PostTitle = "Title No. 3", PostSummary = "Summary No. 3", AddDate = new DateTime(2010, 2, 18) });
            _items.Add(new Post() { PostID = 4, PostTitle = "Title No. 4", PostSummary = "Summary No. 4", AddDate = new DateTime(2010, 2, 19) });
            return new RssResult(_items);


And that's it. You can now point to and VOILA!!!!!


Post Comment
Showing all comments

como bajo proxy server

Posted: Jan 23, 2014 @06:45 PM

Pretty simple and easy to implement.

Posted: Aug 26, 2010 @02:09 AM

RSS Feed

Quick Links