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.
Step 2
Select a Web API template from the template wizard. The options for MVC and Web API are selected as shown below:
Step 3
An ASP.NET project will be created and the solution in the explorer looks like the following:
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.
Select Web API 2 Controller with read/write actions and name it as RSSFeedController as shown below.
Step 5: In the RSSFeed controller, write a class called RSSRepository with a few properties.
Step 6: Modify the existing controller action (in the demo), use HTTPResponse as a return type as in the following:
- HttpResponseMessage represents the Represents a HTTP response message.
- public HttpResponseMessage Get()
- {
- var output = new MemoryStream();
- string xml;
- try
- Next, I have written a logic to get data sample.
- List<RSSRepository> Feeds = new List<RSSRepository>();
- Feeds.Add(new RSSRepository()
- {
- Id="[email protected]",
- Content="Sample RSS Feed to display.",
- Link = "http://www.c-sharpcorner.com/authors/aravindbenator/articles/",
- Title="Aravind-Aravindbenator",
- UpdatedOn=DateTime.Now
- });
- List<SyndicationItem> items = new List<SyndicationItem>();
Next, create a syndication feed with header as in the following:
- List<SyndicationItem> items = new List<SyndicationItem>();
- var feed = new SyndicationFeed("RSS Feed Sample-Web API", "Aravind.", new Uri("http://www.c-sharpcorner.com/authors/aravindbenator/articles/"));
- feed.Categories.Add(new SyndicationCategory("RSS Feed Sample-Web API"));
Create syndication items as in the following:
- var solutionfeed = new SyndicationItem(rssrepo.Title, rssrepo.Content,
- new Uri("http://www.c-sharpcorner.com/authors/aravindbenator/articles/"), "[email protected]",
- rssrepo.UpdatedOn);
- solutionfeed.Title = new TextSyndicationContent(rssrepo.Title);
- solutionfeed.Id = "[email protected]";
- solutionfeed.BaseUri = new Uri("http://www.c-sharpcorner.com/authors/aravindbenator/articles/");
- solutionfeed.Content = new TextSyndicationContent(rssrepo.Content );
-
- solutionfeed.ElementExtensions.Add(new XElement("enclosure", new XAttribute("type", ""),
- new XAttribute("url", "http://www.c-sharpcorner.com/UploadFile/AuthorImage/aravindbenator.jpg"), new XAttribute("width", 200),
- new XAttribute("height", 200)).CreateReader());
- 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:
- var formatter = new Rss20FeedFormatter(feed);
- var xws = new XmlWriterSettings { Encoding = Encoding.UTF8 };
- using (var xmlWriter = XmlWriter.Create(output, xws))
- {
- formatter.WriteTo(xmlWriter);
- xmlWriter.Flush();
- }
- using (var sr = new StreamReader(output))
- {
- output.Position = 0;
- xml = sr.ReadToEnd();
- sr.Close();
- }
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.
Thanks and regards.
Aravind