Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Created June 14, 2017 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BryanWilhite/dde3afad9813fe79f2f3f71431310481 to your computer and use it in GitHub Desktop.
Save BryanWilhite/dde3afad9813fe79f2f3f71431310481 to your computer and use it in GitHub Desktop.
ASP.NET Web API RSS Feed Controller
using Newtonsoft.Json.Linq;
using Songhay.Extensions;
using Songhay.Web.Extensions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web.Http;
using System.Xml;
namespace Songhay.Dashboard.Controllers
{
/// <summary>
/// <see cref="SyndicationFeed"/> Controller
/// </summary>
/// <seealso cref="ApiController" />
[RoutePrefix("api")]
public class FeedController : ApiController
{
/// <summary>
/// Initializes a new instance of the <see cref="FeedController"/> class.
/// </summary>
public FeedController()
{
var feedsPath = ConfigurationManager.AppSettings.GetPathFromAppSettings("SyndicationFeeds", typeof(Global));
var jO = JObject.Parse(File.ReadAllText(feedsPath));
this._feeds = jO.GetDictionary("feeds");
}
/// <summary>
/// Gets the feed.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
/// <exception cref="System.NullReferenceException">The expected Syndication feeds are not here.</exception>
/// <exception cref="System.IndexOutOfRangeException">The expected feed key is not here.</exception>
[Route("feed/{id}")]
public SyndicationFeed GetFeed(string id)
{
if (this._feeds == null) throw new NullReferenceException("The expected Syndication feeds are not here.");
if (!this._feeds.Keys.Contains(id)) throw new IndexOutOfRangeException("The expected feed key is not here.");
var feedUri = new Uri(this._feeds[id]);
SyndicationFeed feed;
using (var reader = XmlReader.Create(feedUri.AbsoluteUri))
{
feed = SyndicationFeed.Load(reader);
}
return feed;
}
Dictionary<string, string> _feeds;
}
}
{
"feeds": {
"codepen": "http://codepen.io/rasx/public/feed/",
"flickr": "http://api.flickr.com/services/feeds/photoset.gne?set=72157625087343217&nsid=7160940@N02&format=rss2&lang=en-us",
"studio": "http://songhayblog.azurewebsites.net/feed?formatter=rss"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment