Skip to content

Instantly share code, notes, and snippets.

@demndevel
Created November 2, 2022 07:19
Show Gist options
  • Save demndevel/f4f4a3f380057960f225e34995501372 to your computer and use it in GitHub Desktop.
Save demndevel/f4f4a3f380057960f225e34995501372 to your computer and use it in GitHub Desktop.
Rss feed in asp.net mvc core controller example
public class RssController : Controller
{
private readonly INoteRepository _notes;
public RssController(INoteRepository notes)
{
_notes = notes;
}
public ContentResult Rss()
{
var lastTenNotes = _notes.GetArray().OrderByDescending(n => n.Date).Take(10).ToList();
var xml = BuildXmlFeed($"{Request.Scheme}://{Request.Host}", lastTenNotes);
return new ContentResult
{
ContentType = "application/xml",
Content = xml,
StatusCode = 200
};
}
private string BuildXmlFeed(string url, List<Note> notes)
{
StringWriter parent = new StringWriter();
using (XmlTextWriter writer = new XmlTextWriter(parent))
{
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");
writer.WriteStartElement("channel");
writer.WriteElementString("title", $"demn's blog");
writer.WriteElementString("link", url);
writer.WriteElementString("ttl", "60");
writer.WriteStartElement("atom:link");
writer.WriteAttributeString("href", url);
writer.WriteAttributeString("rel", "self");
writer.WriteAttributeString("type", "application/rss+xml");
writer.WriteEndElement();
foreach (var note in notes)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", note.Title);
writer.WriteElementString("pubDate",
note.Date.ToString("ddd, dd MMM yyyy", CultureInfo.GetCultureInfo("en-US")) + " 00:00:00 +0000");
writer.WriteElementString("link", $"{url}/note/{note.Id}");
writer.WriteElementString("description", note.ShortDescription);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
}
return parent.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment