This is a viewer only at the moment see the article on how this works.
To update the preview hit Ctrl-Alt-R (or ⌘-Alt-R on Mac) or Enter to refresh. The Save icon lets you save the markdown file to disk
This is a preview from the server running through my markdig pipeline
إضافة RSS مع ASP.NET Core
Wednesday, 07 August 2024
المقدمة
RSS (and Atom) remains the only widely adopted format for syndicating contentM SK2 It 's a simple XML format that can be consumed by a broad range of feed readersMSC4 In this postMNK5 IMRK6ll show you how to add an RSS feed to your ASPMMK7NET Core applicationMسک8
إنشاء المغذيات
حقاً جوهر هذا هو إنشاء وثيقة XML للغذاء RSS.
الشيفرة التالية تأخذ قائمة من RssFeedItem ينتج XML للغذاء. RssFeedItem الصنف هو صنف بسيط يمثل عنصرا في المدخل.
public string GenerateFeed(IEnumerable<RssFeedItem> items, string categoryName = "")
{
XNamespace atom = "http://www.w3.org/2005/Atom";
var feed = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("rss", new XAttribute(XNamespace.Xmlns + "atom", atom.NamespaceName), new XAttribute("version", "2.0"),
new XElement("channel",
new XElement("title", !string.IsNullOrEmpty(categoryName) ? $"mostlylucid.net for {categoryName}" : $"mostlylucid.net"),
new XElement("link", $"{GetSiteUrl()}/rss"),
new XElement("description", "The latest posts from mostlylucid.net"),
new XElement("pubDate", DateTime.UtcNow.ToString("R")),
new XElement(atom + "link",
new XAttribute("href", $"{GetSiteUrl()}/rss"),
new XAttribute("rel", "self"),
new XAttribute("type", "application/rss+xml")),
from item in items
select new XElement("item",
new XElement("title", item.Title),
new XElement("link", item.Link),
new XElement("guid", item.Guid, new XAttribute("isPermaLink", "false")),
new XElement("description", item.Description),
new XElement("pubDate", item.PubDate.ToString("R")),
from category in item.Categories
select new XElement("category", category)
)
)
)
);
var settings = new XmlWriterSettings
{
Indent = true,
Encoding = new UTF8Encoding(false) // UTF-8 without BOM
};
using (var memoryStream = new MemoryStream())
using (var writer = XmlWriter.Create(memoryStream, settings))
{
feed.Save(writer);
writer.Flush();
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
أشياء مهمة في الرمز الأعلى:
نحن بحاجة إلى إنشاء إسم namespace Atom وحقنه في مستند XML لدعم atom:link عنصر.
XNamespace atom = "http://www.w3.org/2005/Atom";
var feed = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("rss", new XAttribute(XNamespace.Xmlns + "atom", atom.NamespaceName), new XAttribute("version", "2.0"),
البرمجة UTF-8
على الرغم من أننا نحدد utf-8 هنا حسناً ASP.NET Core يتجاهل ذلك ...لأنه 'خاصة . عوضاً عن ذلك نحتاج أن نتأكد من أن الأوتار التي تنتج في الوثيقة هي في الواقع UTF
var settings = new XmlWriterSettings
{
Indent = true,
Encoding = new UTF8Encoding(false) // UTF-8 without BOM
};
لكن لا نأمل ...علينا أن نفعل هذا :
using (var memoryStream = new MemoryStream())
using (var writer = XmlWriter.Create(memoryStream, settings))
{
feed.Save(writer);
writer.Flush();
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
بهذه الطريقة ينتج بالفعل وثيقة XML مع utf-8 التشفير.
المتحكم
ومن هناك "مسك0" بسيط جداً في نظام التحكم RSS "مسک1" لدينا طريقة تقبل category و startdate (currently a super secret feature ??) and returns the feed
[HttpGet]
public IActionResult Index([FromQuery] string category = null, [FromQuery] string startDate = null)
{
DateTime? startDateTime = null;
if (DateTime.TryParseExact(startDate, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None,
out DateTime startDateTIme))
{
logger.LogInformation("Start date is {startDate}", startDate);
}
var rssFeed = rssFeedService.GenerateFeed(startDateTime, category);
return Content(rssFeed, "application/rss+xml", Encoding.UTF8);
}
ثم في `_التصميم.cshtml' نضيف رابطاً إلى المزود
<link rel="alternate" type="application/rss+xml"
title="RSS Feed for mostlylucid.net"
href="/rss" />
هذا يضمن أن تغذي يمكن 'إكتشافها بنفسك ' من قبل قارئات تغذي