How to create a syndication (Rss) feed in asp.net MVC4 application


In this post, I am explain How to create a syndication (Rss) feed in asp.net MVC4 application
Syndication feeds are commonly used by websites to display or expose RSS/ATOM feed for latest content like headlines , articles, posts, news etc, which is a specially formatted XML file that includes a summary of the most recently published items. The two most popular syndication feed standards are RSS 2.0 and Atom 1.0. Here in this example I have explained how to create a syndication (Rss + Atom) feed in asp.net MVC4 application.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Basic Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a reference "System.ServiceModel" .

Go to Solution Explorer > Right Click on References > Add reference> select "System.ServiceModel" > Ok.

Step-6: Add a class for extending ActionResult for return Rss Feeds.

Go to Solution Explorer > Right Click on Project > Add > Enter Class Name > Add

Here In this example I have created "RSSResult" class.
Write following code in the class...
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace MvcRSSFeedCreate
    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}))
                //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}))

Step-7: Create a Controller .

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Here I have created a controller "HomeController"

Step-8: Add new action into your controller for Get Rss Feed

Here I have added "RssFeed" Action into "Home" Controller. Please write this following code

public ActionResult RssFeed()
    SyndicationFeed feed = null;
    string siteTitle, description, siteUrl;
    siteTitle = "Dotnet Awesome";
    siteUrl = "http://dotnetawesome.com";
    description = "Welcome to the dotnetawesome.com! We are providing step by step tutorial of ASP.NET Web Forms, ASP.NET MVC, Jquery in ASP.NET and about lots of control available in asp.net framework & topic  like GridView, webgrid, mvc4, DropDownList, AJAX, Microsoft, Reports, .rdlc,    mvc, DetailsView, winforms, windows forms, windows application, code, .net code, examples, WCF, tutorial, WebService, LINQ and more. This is the best site for beginners as well as for advanced learner.";

    using (MyDatabaseEntities dc = new MyDatabaseEntities())
        List<SyndicationItem> items = new List<SyndicationItem>();
        //Last 10 Post
        var c = dc.MyPosts.OrderByDescending(a => a.PostID).Take(10).ToList();
        foreach (var i in c)
            SyndicationItem item = new SyndicationItem
                    Title = new TextSyndicationContent(i.Title),
                    Content = new TextSyndicationContent(GetPlainText(i.Content,200)), //here content may be Html content so we should use plain text
                    PublishDate = i.PublishDate
            item.Links.Add(new SyndicationLink(new Uri(Request.Url.Scheme + "://" + Request.Url.Host + i.LiveURL)));

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

    return new RSSResult { feedData = feed };
here I have created a function for Get Get Plain Text here...
private string GetPlainText(string htmlContent, int length = 0)
    string HTML_TAG_PATTERN = "<.*?>";
    string plainText = Regex.Replace(htmlContent, HTML_TAG_PATTERN, string.Empty);
    return length > 0 && plainText.Length > length ? plainText.Substring(0, length) : plainText;

Step-9: Run Application.

Download  Live Demo

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.