using System.ServiceModel.Syndication; using System.Xml; using System.IO; using System.Text;
// Customize this method with your real data. private IEnumerable<Post> GetBlogPosts() { var posts = new List<Post>(); posts.Add(new Post() { Title = "A Professional ASP.NET Core - RSS", UrlSlug = "a-professional-asp.net-core-rss", Description = "RSS feeds provide an excellent mechanism for websites to publish their content for consumption by others.", CreatedDate = new DateTime(2020, 10, 9) }); posts.Add(new Post() { Title = "A Professional ASP.NET Core API - Caching", UrlSlug = "a-professional-asp.net-core-api-caching", Description = "Caching is a technique of storing the frequently accessed/used data so that the future requests for those sets of data can be served much faster to the client..", CreatedDate = new DateTime(2020, 10, 5) }); posts.Add(new Post() { Title = "Using Tailwind CSS with Aurelia 2 and Webpack", UrlSlug = "aurelia-2-with-tailwindcss-and-webpack", Description = "Tailwind CSS is a highly customizable, low-level CSS framework that gives you all of the building blocks you need to build bespoke designs without any annoying opinionated styles you have to fight to override.", CreatedDate = new DateTime(2020, 7, 23) }); return posts; }
// http://localhost:PORT/rss.xml [ResponseCache(Duration = 1200)] [HttpGet("/rss.xml")] public IActionResult Rss() { var feed = new SyndicationFeed("Title", "Description", new Uri("https://hamedfathi.me"), "RSSUrl", DateTime.Now); feed.Copyright = new TextSyndicationContent($"{DateTime.Now.Year} Hamed Fathi"); var items = new List<SyndicationItem>(); var postings = GetBlogPosts(); foreach (var item in postings) { var postUrl = Url.Action("Article", "Blog", new { id = item.UrlSlug }, HttpContext.Request.Scheme); var title = item.Title; var description = item.Description; items.Add(new SyndicationItem(title, description, new Uri(postUrl), item.UrlSlug, item.CreatedDate)); } feed.Items = items; var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, NewLineHandling = NewLineHandling.Entitize, NewLineOnAttributes = true, Indent = true }; using (var stream = new MemoryStream()) { using (var xmlWriter = XmlWriter.Create(stream, settings)) { var rssFormatter = new Rss20FeedFormatter(feed, false); rssFormatter.WriteTo(xmlWriter); xmlWriter.Flush(); } return File(stream.ToArray(), "application/rss+xml; charset=utf-8"); } }
The only important note here is that I am setting a ResponeCache attribute on this method. I strongly recommend this as your RSS Feed generation is often something that will be going to the database etc. and feed-readers are notorious for multiple refreshes etc. By enabling ResponseCaching for your RSS action, you can reduce the load on your server.
Fetch RSS feed
You are able to loading and processing the feed.
1 2 3 4 5 6 7 8 9 10
using System.ServiceModel.Syndication; using System.Xml;
public IEnumerable<SyndicationItem> GetFeedItems(string rssUrl) { var reader = XmlReader.Create(rssUrl); var feed = SyndicationFeed.Load(reader); var posts = feed.Items; return posts; }
RSS Middleware
Snickler.RSSCore was created in order to provide a way to easily generate RSS feeds on the fly via an ASP.NET Core Middleware.
Set the options for your RSS Feed in Configure with UseRSSFeed
1 2 3 4 5 6 7 8 9 10 11 12
publicvoidConfigure(IApplicationBuilder app, IHostingEnvironment env) { app.UseRssFeed("/feed", new RSSFeedOptions { Title = "Snickler's Super Awesome RSS Feed", Copyright = "2018", Description = "The Best and Most Awesome RSS Feed Content", ManagingEditor = "managingeditor@someaddress.com", Webmaster = "webmaster@someaddress.com", Url = new Uri("http://someaddress.com") }); }
Optional RSS Caching Configuration
By default, MemoryCache is used to cache the feed for 1 day. To be able to update the cache duration or cache key, add a new instance of the MemoryCacheProvider to the Caching property within the RSSFeedOptions class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
app.UseRssFeed("/feed", new RSSFeedOptions { Title = "Snickler's Super Awesome RSS Feed", Copyright = "2018", Description = "The Best and Most Awesome RSS Feed Content", ManagingEditor = "managingeditor@someaddress.com", Webmaster = "webmaster@someaddress.com", Url = new Uri("http://someaddress.com"), Caching = new MemoryCacheProvider { CacheDuration = TimeSpan.FromDays(5), CacheKey = "SomeSuperAwesomeCacheKey" } });
Reference(s)
Most of the information in this article has gathered from various references.