Today, we are going to discuss about create RSS feed on ASP.NET MVC 6 Hosting. The RSS classes that we will be describing are available in all types of ASP.NET applications, not only the web-based onces. But we think displaying a blog item on your website is great and relatively simple example of consuming RSS feeds in ASP.NET.

There actually is built-in RSS support in ASP.NET. We have not been aware of that support for some time and occasionally used the third-party library RSS.NET. It turns out that we do not need a separate library any longer. Here is an example MVC controller that refers to the SyndicationFeed and SyndicationItem classes in its Index action method.

using System.Web.Mvc;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Linq;namespace Antrix.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            const string feedUrl =
            "http://yourdomain.com/feeds/posts/default?alt=rss";
           SyndicationFeed feed = null;
           using (XmlReader reader = XmlReader.Create(feedUrl))
            {
                feed = SyndicationFeed.Load(reader);
            }
            if (feed != null)
            {
                SyndicationItem item = feed.Items.First<SyndicationItem>();

                ViewBag.RssItem = item;         
            }
           
             return View();
        }
        public ActionResult About()
        {
            return View();
       }
    }
}

The Index method will reads a feed of the site that you are reading right now. It puts the first item (which we assume represents the last post that has been published) in the ViewBag. Normally we use model classes to refer to data within my MVC views, but we want to keep things simple. We have added code to show the title and summary of the last post, and a link to the full post.

@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>

    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<div>

From our blog:  
<div style="font-weight: bold; padding: 10px">@ViewBag.RssItem.Title.Text</div>
<div style="clear: both; padding: 10px">@Html.Raw(@ViewBag.RssItem.Summary.Text)</div>
<div style="clear: both; padding: 10px"><a href="@ViewBag.RssItem.Links[0].Uri" target="_blank">Show full post</a></div>
</div>

Using the SyndicationFeed and SyndicationItem classes it becomes rather easy to create your own web-based (or Windows-based, if you prefer) RSS reader.