I am working in Asp.net Core 1.0 MVC 6
I am trying to write a component to provide RSS feeds from my websites.
I found this post that suggests that System.ServiceModel.Syndication has yet to be ported to ASP.NET CORE.
I am unable to target the full .NET framework.
The suggestion is to write as an xml parser. I am however struggling to get my head around everything that might be required.
I have built the functionality to get my data into XML but now need to better understand how to allow this to be called from an IActionResult (or indeed how to generate a link that may be placed on my page).
I can provide samples of my code but am not sure it'll be helpful.
Is anyone able to point me in the right direction of examples achieving this?
I also found an answer on this post which points towards some ideas but I think is pre MVC6/Asp.net Core: RSS Feeds in ASP.NET MVC
They've released a preview NuGet package for Microsoft.SyndicationFeed. On the dotnet/wcf repo, they've provided an example to get you up and running.
EDIT: I've posted a Repo and a NuGet package that serves as a Middleware around this.
// action to return the feed
[Route("site/GetRssFeed/{type}")]
public IActionResult GetRssFeed(ArticleStatusTypes type)
{
var feed = _rss.BuildXmlFeed(type);
return Content(feed, "text/xml");
}
public string BuildXmlFeed(ArticleStatusTypes type)
{
var key = $"RssFeed{Convert.ToInt32(type)}{_appInfo.ApplicationId}";
var articles =
_cache.GetCachedData(key) ??
_cache.SetCache(key, _service.GetItems(Convert.ToInt32(type), _appInfo.CacheCount));
StringWriter parent = new StringWriter();
using (XmlTextWriter writer = new XmlTextWriter(parent))
{
writer.WriteProcessingInstruction("xml-stylesheet", "title=\"XSL_formatting\" type=\"text/xsl\" href=\"/skins/default/controls/rss.xsl\"");
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");
// write out
writer.WriteStartElement("channel");
// write out -level elements
writer.WriteElementString("title", $"{_appInfo.ApplicationName} {type}" );
writer.WriteElementString("link", _appInfo.WebsiteUrl);
//writer.WriteElementString("description", Description);
writer.WriteElementString("ttl", "60");
writer.WriteStartElement("atom:link");
//writer.WriteAttributeString("href", Link + Request.RawUrl.ToString());
writer.WriteAttributeString("rel", "self");
writer.WriteAttributeString("type", "application/rss+xml");
writer.WriteEndElement();
if (articles != null)
{
foreach (var article in articles)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", article.Title);
writer.WriteElementString("link", _appInfo.WebsiteUrl); // todo build article path
writer.WriteElementString("description", article.Summary);
writer.WriteEndElement();
}
}
// write out
writer.WriteEndElement();
// write out
writer.WriteEndElement();
}
return parent.ToString();
}