William Duffy

Glasgow Based C# ASP.NET Web Developer

Creating a simple RSS feed using ASP.NET MVC

Looking to serve an rss document via your ASP.NET MVC controller? Here is a simple, quick means to do so. If you are looking for a much neater, smarter, reusable method of serving rss from your controllers check out my ASP.NET MVC RssResult post (coming soon).

For the demonstrations below I will be using a generic List<News>. News is a simple class with common news properties, we will be using Title, Description and Url. Although you can change the view to handle your own model easily enough.

Create a view named Feed, as you would normally for an HTML view. However, in this case change the ContentType property to “text/xml”. Sample code is listed below. Notice the <?xml tag is butted up against the page declaration. This is necessary because an xml document’s first character must be the beginning of the doctype declaration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
<%@ Page Language="C#" ="text/xml" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Domain.Model.News>>" %><?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>William Duffy's News</title>
    <link>http://www.wduffy.co.uk</link>
    <description>The latest news and stuff from the exciting life of William Duffy</description>
    <% foreach (var news in Model) { %>
    <item>
      <title><%= news.Title %></title>
      <link>http://www.wduffy.co.uk/News/Details/<%= news.Url %></link>
      <description><%= news.Description %></description>
    </item>
    <% } %>
  </channel>
</rss>

In your controller, pass the IEnumerable collection of items that are to be represented in the rss document to the view.

 
public ActionResult Feed()
{
    NewsCollection news = new NewsService().GetByLatest();
    return View(news);
}

And that’s it. You can now request your rss document at http://www.yourdomain.com/Controller/Feed.


Categorized as Uncategorized

7 Comments

  1. Or you could use .NET’s built-in SyndicationFeed class. Then use either the Rss20FeedFormatter or the Atom10FeedFormatter classes to serialize the SyndicationFeed to either RSS or Atom. No need to create any views or know about the proper XML format of RSS or ATOM feeds.

  2. Nice Patrick, I never even knew that existed. Normally I just build my rss feeds using an xml writer but will need to check out your recommendation in more detail.

  3. Hey guys. I have this tiny bit problem stuck here. See, I have finished my RSS feed, tested it and all works perfectly, but when I remove an article from my feed it still showed in the reader. How do i get the reader to see the article is no longer part of the feed. Any ideas what’s wrong?

  4. Canaan, it sound like you have a cached feed somewhere between the view and the source. Maybey check to ensure that a fresh request is being made every time if you need to the second accuracy in the feed.

  5. I have used this method before, but using XML writer was far better for me as the results seems to be more accurate with it and even easy coding. This could have been more cool to have some LINQ to XML.

  6. I am a web developer and just few days ago i m trying to fetch rss but could not do it why i dont know. But now i am going to create new rss from using ASP.NET MVC and then lets see. thanks for your post

comment_type == "trackback" || $comment->comment_type == "pingback" || ereg("", $comment->comment_content) || ereg("", $comment->comment_content)) { ?>

Trackbacks & Pingbacks

  1. Patrick Steele

    Or you could use .NET’s built-in SyndicationFeed class. Then use either the Rss20FeedFormatter or the Atom10FeedFormatter classes to serialize the SyndicationFeed to either RSS or Atom. No need to create any views or know about the proper XML format of RSS or ATOM feeds.

  2. comment_type == "trackback" || $comment->comment_type == "pingback" || ereg("", $comment->comment_content) || ereg("", $comment->comment_content)) { ?>

    Trackbacks & Pingbacks

    1. William

      Nice Patrick, I never even knew that existed. Normally I just build my rss feeds using an xml writer but will need to check out your recommendation in more detail.

    2. comment_type == "trackback" || $comment->comment_type == "pingback" || ereg("", $comment->comment_content) || ereg("", $comment->comment_content)) { ?>

      Trackbacks & Pingbacks

      1. RssResult - An ASP.NET MVC RSS ActionResult | William Duffy

        [...] I posted the article Creating a simple RSS feed using ASP.NET MVC which demonstrated how to create an RSS feed in a quick, easy way. Here, I will show you how to [...]

      2. comment_type == "trackback" || $comment->comment_type == "pingback" || ereg("", $comment->comment_content) || ereg("", $comment->comment_content)) { ?>

        Trackbacks & Pingbacks

        1. Canaan Moore

          Hey guys. I have this tiny bit problem stuck here. See, I have finished my RSS feed, tested it and all works perfectly, but when I remove an article from my feed it still showed in the reader. How do i get the reader to see the article is no longer part of the feed. Any ideas what’s wrong?

        2. comment_type == "trackback" || $comment->comment_type == "pingback" || ereg("", $comment->comment_content) || ereg("", $comment->comment_content)) { ?>

          Trackbacks & Pingbacks

          1. William

            Canaan, it sound like you have a cached feed somewhere between the view and the source. Maybey check to ensure that a fresh request is being made every time if you need to the second accuracy in the feed.

          2. comment_type == "trackback" || $comment->comment_type == "pingback" || ereg("", $comment->comment_content) || ereg("", $comment->comment_content)) { ?>

            Trackbacks & Pingbacks

            1. David Grant

              I have used this method before, but using XML writer was far better for me as the results seems to be more accurate with it and even easy coding. This could have been more cool to have some LINQ to XML.

            2. comment_type == "trackback" || $comment->comment_type == "pingback" || ereg("", $comment->comment_content) || ereg("", $comment->comment_content)) { ?>

              Trackbacks & Pingbacks

              1. Dag Magnusson

                I am a web developer and just few days ago i m trying to fetch rss but could not do it why i dont know. But now i am going to create new rss from using ASP.NET MVC and then lets see. thanks for your post

              Leave a Reply