Simple Way to Create RSS Feed With Web API

RSS Feeds

The following describes Really Simple Syndication (RSS):

  • RSS Syndicates site content.
  • Defines an easy way to share and view headlines and content.
  • RSS files can be automatically updated.
  • RSS is XML.

Implementing RSS Feed with Web API


There are many more articles flying on the internet about implementing RSS Feed.  But here, I am providing a simple way to implement RSS feed with the Web API.

Step 1

 

Open Visual Studio and create ASP.NET web application.

Creating Web Application

Step 2

Select a Web API template from the template wizard. The options for MVC and Web API are selected as shown below:

Web Api Project Template

Step 3

An ASP.NET project will be created and the solution in the explorer looks like the following:

Areas in MVC

Now, right-click on the Areas folder and Add New and Controller option.

Step 4: A wizard with options to add various types of API controller will be displayed.

Adding Web API 2 Controller

Select Web API 2 Controller with read/write actions and name it as RSSFeedController as shown below.

Areas Folder in MVC

Step 5: In the RSSFeed controller, write a class called RSSRepository with a few properties.

Class Code View

Step 6: Modify the existing controller action (in the demo), use HTTPResponse as a return type as in the following:

  1. HttpResponseMessage represents the Represents a HTTP response message.  
  2. public HttpResponseMessage Get()  
  3. {  
  4.     var output = new MemoryStream();  
  5.     string xml;  
  6.     try  
  7.     Next, I have written a logic to get data sample.  
  8.     List<RSSRepository> Feeds = new List<RSSRepository>();  
  9.     Feeds.Add(new RSSRepository()  
  10.     {  
  11.         Id="[email protected]",  
  12.         Content="Sample RSS Feed to display.",  
  13.         Link = "http://www.c-sharpcorner.com/authors/aravindbenator/articles/",  
  14.         Title="Aravind-Aravindbenator",  
  15.         UpdatedOn=DateTime.Now  
  16.     });  
  17.     List<SyndicationItem> items = new List<SyndicationItem>();  

Class View

Remaining Class

Next, create a syndication feed with header as in the following:

  1. List<SyndicationItem> items = new List<SyndicationItem>();  
  2. var feed = new SyndicationFeed("RSS Feed Sample-Web API""Aravind."new Uri("http://www.c-sharpcorner.com/authors/aravindbenator/articles/"));  
  3. feed.Categories.Add(new SyndicationCategory("RSS Feed Sample-Web API"));  
Create syndication items as in the following:
  1. var solutionfeed = new SyndicationItem(rssrepo.Title, rssrepo.Content,  
  2. new Uri("http://www.c-sharpcorner.com/authors/aravindbenator/articles/"), "[email protected]",  
  3. rssrepo.UpdatedOn);  
  4. solutionfeed.Title = new TextSyndicationContent(rssrepo.Title);  
  5. solutionfeed.Id = "[email protected]";  
  6. solutionfeed.BaseUri = new Uri("http://www.c-sharpcorner.com/authors/aravindbenator/articles/");  
  7. solutionfeed.Content = new TextSyndicationContent(rssrepo.Content );  
  8.   
  9. solutionfeed.ElementExtensions.Add(new XElement("enclosure"new XAttribute("type"""),   
  10. new XAttribute("url""http://www.c-sharpcorner.com/UploadFile/AuthorImage/aravindbenator.jpg"), new XAttribute("width", 200),   
  11. new XAttribute("height", 200)).CreateReader());  
  12. items.Add(solutionfeed);  
Add syndication items to syndicate the feeds. I have added a few sample data to the syndication item for reference.

feed.Items = items;

Step 7

The following shows formatting the Syndication Feed:

  1. var formatter = new Rss20FeedFormatter(feed);  
  2. var xws = new XmlWriterSettings { Encoding = Encoding.UTF8 };  
  3. using (var xmlWriter = XmlWriter.Create(output, xws))  
  4. {  
  5.     formatter.WriteTo(xmlWriter);  
  6.     xmlWriter.Flush();  
  7. }  
  8. using (var sr = new StreamReader(output))  
  9. {  
  10.     output.Position = 0;  
  11.     xml = sr.ReadToEnd();  
  12.     sr.Close();  
  13. }  

The following shows returning the RSS Feed:

var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(xml, Encoding.UTF8, "application/atom+xml") };
return response;

Step 8

The following shows testing the RSS Feed,

The URL will be like http://localhost:50945/api/RSSFeed (Base URL + API +Controller).

You can use custom methods too.

Rss Feed View in Browser

Thanks and regards.

Aravind


Similar Articles