在asp.net core 1.0中创建RSS feed

问题描述 投票:3回答:2

我在Asp.net Core 1.0 MVC 6工作我正在尝试编写一个组件来从我的网站提供RSS源。

我发现this post表明System.ServiceModel.Syndication尚未移植到ASP.NET CORE。

我无法定位完整的.NET框架。

建议是编写为xml解析器。然而,我正在努力探索可能需要的一切。

我已经构建了将数据转换为XML的功能,但现在需要更好地了解如何从IActionResult中调用它(或者实际上如何生成可能放在我页面上的链接)。

我可以提供我的代码示例,但我不确定它是否有用。是否有人能够指出我实现这一目标的正确方向?

我也在这篇帖子上找到了一个答案,指出了一些想法,但我认为是MVC6 / Asp.net核心:RSS Feeds in ASP.NET MVC

asp.net xml asp.net-core-mvc asp.net-core-1.0 rss2
2个回答
3
投票

他们为Microsoft.SyndicationFeed发布了预览NuGet包。在dotnet / wcf repo上,他们提供了一个example来启动和运行。

编辑:我发布了一个Repo和一个NuGet package作为中间件围绕这个。


3
投票
// 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();
}
© www.soinside.com 2019 - 2024. All rights reserved.