简体   繁体   中英

Render pubDate with RSS SyndicationFeed api

I am trying to serve an RSS feed from my ASP.net MVC site. Everything is working accept for the pubdate element. I can't seem to get the Rss20FeedFormatter to output it. I thought that it maps to the LastUpdatedDate property on the SyndicationFeed object, but that outputs as LastBuildDate.

Does anyone know how I can use the SyndicationFeed with Rss20FeedFormatter to render the pubDate node in my RssFeed?

   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);
        }
    }

example of how I am creating the feed.

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

It seems that the object model currently supports pubDate only on items, not on the feed/channel. You can add it as an ElementExtension:

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

You just have to take care to format the date correctly, have a look here: DateTime to RFC-1123 gives inaccurate timezone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM