Created
June 14, 2017 20:52
-
-
Save BryanWilhite/dde3afad9813fe79f2f3f71431310481 to your computer and use it in GitHub Desktop.
ASP.NET Web API RSS Feed Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment