ASP.NET MVC

RSS Feed Using .NET MVC

What is RSS?

An RSS (Really Simple Syndication) is an online file that includes a description of every piece of content which the site has published. The site publishes a new blog about that content—including the Description of the blog or a summary, publication date, author, link and etc.—are automatically generated in the file and displayed in reverse chronological order.

Since blogs are updating with details about every piece of content a site publishes, you can use RSS feeds to keeping up to date with every new item your favorite blog publishes or automatically generating email letters or social media posts to boost your new content.

RSS feeds are in XML format. It looks like this one:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Vision Infotech | The Ultimate It Solution</title>
    <link>https://www.visioninfotech.net/</link>
    <description>We aim at translating clients IT vision into reality and craft powerful. Vision Infotech is an organization that provides strategic business solutions and develop.</description>
    <item>
      <title>How to Find book?</title>
      <description>abcdefghijklmnopqrstuvwxyz1234567890</description>
      <pubDate>Sat, 23 Jan 2021 15:30:55 +0530</pubDate>
      <a10:link href="http://localhost/How-to-find-book" />
    </item>
    <item>
      <title>Why Read this book?</title>
      <description>0987654321abcdefghijklmnopqrstuvwxyz</description>
      <pubDate>Sat, 23 Jan 2021 15:30:55 +0530</pubDate>
      <a10:link href="http://localhost/Why-read-this-book" />
    </item>
  </channel>
</rss>

 

Implement Method to Generate RSS Feed

Step 1: Install-Package System.ServiceModel.Syndication

 

Step 2: Add the following class to fetch the Blogs details :

public class BlogVM
{
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime PublishDate { get; set; }
    public string URLTitle { get; set; }
}

 

Step 3: Add the following class to convert the Feed data in XML format :

public class RSSResult : ActionResult
{
    public SyndicationFeed feedData { get; set; }
    public string contentType = "rss";

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "application/atom+xml";
        //check request is for Atom or RSS
        if (context.HttpContext.Request.QueryString["type"] != null && context.HttpContext.Request.QueryString["type"].ToString().ToLower() == "atom")
        {
            //Atom Feed
            context.HttpContext.Response.ContentType = "application/atom+xml";
            var rssFormatter = new Atom10FeedFormatter(feedData);
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output, new XmlWriterSettings { Indent = true }))
            {
                rssFormatter.WriteTo(writer);
            }
        }
        else
        {
            //RSS Feed
            context.HttpContext.Response.ContentType = "application/rss+xml";
            var rssFormatter = new Rss20FeedFormatter(feedData);
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output, new XmlWriterSettings { Indent = true }))
            {
                rssFormatter.WriteTo(writer);
            }
        }

    }
}

Make sure to add the following Namespace on the header.

using System.ServiceModel.Syndication;

 

Step 4: Add the following code in the Controller to Call the Method and Get RSS Feed in XML Format :

public ActionResult RSSFeed()
{
    var blogs = new List<BlogVM>() {
        new BlogVM() {
            Title = "How to Find book?", Content = "abcdefghijklmnopqrstuvwxyz1234567890", PublishDate = DateTime.Now, URLTitle = "How-to-find-book"
        },
        new BlogVM() {
            Title = "Why Read this book?", Content = "0987654321abcdefghijklmnopqrstuvwxyz", PublishDate = DateTime.Now, URLTitle = "Why-read-this-book"
        }
    };

    SyndicationFeed feed = null;
    string siteTitle, description, siteUrl;
    siteTitle = "Vision Infotech | The Ultimate It Solution";
    siteUrl = "https://www.visioninfotech.net/";
    description = "We aim at translating clients IT vision into reality and craft powerful. Vision Infotech is an organization that provides strategic business solutions and develop.";

    List<SyndicationItem> items = new List<SyndicationItem>();
    foreach (var blog in blogs)
    {
        SyndicationItem item = new SyndicationItem
        {
            Title = new TextSyndicationContent(blog.Title),
            Content = new TextSyndicationContent(blog.Content),
            PublishDate = DateTimeOffset.Parse(blog.PublishDate.ToString())
        };
        item.Links.Add(new SyndicationLink(new Uri(Request.Url.Scheme + "://" + Request.Url.Host + "/" + blog.URLTitle)));
        items.Add(item);
    }

    feed = new SyndicationFeed(siteTitle, description, new Uri(siteUrl));
    feed.Items = items;

    return new RSSResult { feedData = feed };
}

Make sure to add the following Namespace on the header.

using System.ServiceModel.Syndication;

 

RSS Feed code is now ready. You can call the RSSFeed from the URL and get the XML Feed like :

 

Ghanshyam Godhani

Experienced Full Stack Software Engineer with a demonstrated history of working in the computer software industry.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago