使用SyndicationItem类在rss提要中显示图像

时间:2017-01-02 16:10:29

标签: c# asp.net asp.net-mvc rss syndication

我使用System.ServiceModel.Syndication

创建rss feed
public ActionResult RSS()
        {
            List<C_Node> rssNodes = GetNodeList(takeNum: 20).ToList();
            var syndItems = new List<SyndicationItem>();
            foreach (var item in rssNodes)
            {
                var syndItem = new SyndicationItem()
                {
                    Id = item.NodeId.ToString(),
                    Title = SyndicationContent.CreatePlaintextContent(String.Format("{0}", item.Title)),
                    Summary = SyndicationContent.CreateHtmlContent(HelperMethods.Truncate(item.Details,400)),
                    Content = SyndicationContent.CreateHtmlContent(item.Details),
                    PublishDate = item.PostDate
                };
                //syndItem.ElementExtensions.Add("content:encoded", "", SyndicationContent.CreateHtmlContent(item.Details));
                syndItem.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(ConfigurationManager.AppSettings["SiteUrl"] + Url.Action("Details", "Node", new { id = item.NodeId }))));//Nothing alternate about it. It is the MAIN link for the item.
                syndItems.Add(syndItem);
            }

            return new RssFeed(title: Resources.Site.Title,
                               items: syndItems,
                               contentType: "application/rss+xml",
                               description: Resources.Site.Slogan);
        }

我的问题是如何使用每个联合项目显示图像?

2 个答案:

答案 0 :(得分:2)

完成添加此代码:

syndItem.ElementExtensions.Add(new XElement("image", item.ImageUrl));

答案 1 :(得分:1)

此代码也正常工作

syndItem.ElementExtensions.Add(new XElement("enclosure", new XAttribute("type", "image/jpeg"), new XAttribute("url", item.ImageUrl).CreateReader());