private async Task addChannelIdentityAsync(RssFeedWriter rssFeedWriter)
        {
            await rssFeedWriter.WriteDescription(_feedChannel.FeedDescription.ApplyRle().RemoveHexadecimalSymbols());

            await rssFeedWriter.WriteCopyright(_feedChannel.FeedCopyright.ApplyRle().RemoveHexadecimalSymbols());

            await rssFeedWriter.WriteTitle(_feedChannel.FeedTitle.ApplyRle().RemoveHexadecimalSymbols());

            await rssFeedWriter.WriteLanguage(new CultureInfo(_feedChannel.CultureName));

            await rssFeedWriter.WriteRaw($"<atom:link href=\"{_httpContextInfo.GetRawUrl()}\" rel=\"self\" type=\"application/rss+xml\" />");

            await rssFeedWriter.Write(new SyndicationLink(_httpContextInfo.GetBaseUri(), relationshipType : RssElementNames.Link));
        }
示例#2
0
        public async Task <ActionResult> Feed()
        {
            var entries = await this.unitOfWork.BlogEntries
                          .Include(b => b.Author)
                          .Include(b => b.Tags)
                          .ThenInclude(t => t.Tag)
                          .AsNoTracking()
                          .Where(b => b.Visible && b.PublishDate <= DateTimeOffset.UtcNow)
                          .OrderByDescending(b => b.PublishDate)
                          .ToListAsync();

            string baseUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";

            using (var sw = new StringWriterWithEncoding(Encoding.UTF8))
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings()
                {
                    Async = true, Indent = true
                }))
                {
                    var writer = new RssFeedWriter(xmlWriter);
                    await writer.WriteTitle(this.blogSettings.BlogName);

                    await writer.WriteDescription(this.blogSettings.BlogDescription);

                    await writer.Write(new SyndicationLink(new Uri(baseUrl)));

                    await writer.WriteRaw($"<atom:link href=\"{baseUrl}/Blog/{nameof(this.Feed)}\" rel=\"self\" type=\"application/rss+xml\" xmlns:atom=\"http://www.w3.org/2005/Atom\" />");

                    await writer.Write(new SyndicationImage(new Uri($"{baseUrl}/apple-touch-icon_192.png"))
                    {
                        Title       = this.blogSettings.BlogName,
                        Description = this.blogSettings.BlogDescription,
                        Link        = new SyndicationLink(new Uri(baseUrl))
                    });

                    if (entries.Count > 0)
                    {
                        await writer.WritePubDate(entries[0].PublishDate);
                    }

                    var pipeline = new MarkdownPipelineBuilder()
                                   .UseAdvancedExtensions()
                                   .Build();

                    foreach (var blogEntry in entries)
                    {
                        var syndicationItem = new SyndicationItem()
                        {
                            Id          = blogEntry.Id.ToString(),
                            Title       = blogEntry.Header,
                            Published   = blogEntry.PublishDate,
                            LastUpdated = blogEntry.PublishDate > blogEntry.UpdateDate ? blogEntry.PublishDate : blogEntry.UpdateDate,
                            Description = $"{Markdown.ToHtml(blogEntry.ShortContent, pipeline)}{Markdown.ToHtml(blogEntry.Content, pipeline)}"
                        };

                        syndicationItem.AddLink(new SyndicationLink(new Uri($"{baseUrl}/Blog/{blogEntry.Url}")));

                        syndicationItem.AddContributor(new SyndicationPerson(blogEntry.Author.UserName, blogEntry.Author.Email));

                        foreach (var tag in blogEntry.Tags)
                        {
                            syndicationItem.AddCategory(new SyndicationCategory(tag.Tag.Name));
                        }

                        await writer.Write(syndicationItem);
                    }

                    xmlWriter.Flush();
                }

                return(this.Content(sw.ToString(), "application/rss+xml"));
            }
        }