使用RSS SyndicationFeed api呈现pubDate

时间:2012-05-11 21:38:35

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

我正在尝试从我的ASP.net MVC网站提供RSS源。一切都在接受pubdate元素。我似乎无法让Rss20FeedFormatter输出它。我认为它映射到SyndicationFeed对象上的LastUpdatedDate属性,但输出为LastBuildDate。

有谁知道如何使用SyndicationFeed和Rss20FeedFormatter在我的RssFeed中呈现pubDate节点?

   public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";
            var rssFormatter = new Rss20FeedFormatter(Feed, false);
            using (var writer = XmlWriter.Create(context.HttpContext.Response.Output, new XmlWriterSettings{ Indent = true}))
                rssFormatter.WriteTo(writer);
        }
    }

我如何创建Feed的示例。

new SyndicationFeed("Title", "Description", url, "test_id", publishDate, feedItems){ LastUpdatedTime = publishDate}

1 个答案:

答案 0 :(得分:4)

似乎对象模型目前仅支持pubDate项目,而不支持feed / channel。您可以将其添加为ElementExtension:

feed.ElementExtensions.Add("pubDate", "", DateTime.UtcNow.ToString("r"));

您只需要注意正确设置日期格式,请在此处查看:DateTime to RFC-1123 gives inaccurate timezone