如何格式化SyndicationFeed的结果

时间:2015-09-12 21:04:18

标签: asp.net xml asp.net-mvc razor syndicationfeed

在实现SyndicationFeed以XML格式返回RSS提要后,我找不到格式化从Controller传递给View的结果的解决方案。有无数的解决方案如何使用SyndicationFeed,但几乎没有任何关于在自定义样式中显示它。我怎么能做到这一点?请帮忙!

HomeController.cs

public RssActionResult Index()
    {
        SyndicationFeed mainFeed = new SyndicationFeed();

        foreach (var feed in GetRssFeeds())
        {
            Uri feedUri = new Uri(feed);
            SyndicationFeed syndicationFeed;
            using (XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri))
            {
                syndicationFeed = SyndicationFeed.Load(reader);
            }

            syndicationFeed.Id = feed;
            SyndicationFeed tempFeed = new SyndicationFeed(
                mainFeed.Items.Union(syndicationFeed.Items).OrderByDescending(u => u.PublishDate));

            mainFeed = tempFeed;
        }

        return new RssActionResult() { Feed = mainFeed }; 
    }

public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {

            context.HttpContext.Response.ContentType = "application/rss+xml";
            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);

            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }

    private static List<string> GetRssFeeds()
    {
        List<string> feeds = new List<string>();

        feeds.Add("http://finance.yahoo.com/rss/headline?s=msft,goog,aapl");

        return feeds;
    }

如何在视图中设置样式并显示?最好是在以下内容中

@{
ViewBag.Title = "RSS Feed";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<section id="rssfeed">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-8">
                <h4>The Feed</h4>
                    @* DISPLAY FEED HERE FORMATTED NICELY *@
            </div>
         </div>
     </div>
</section>

0 个答案:

没有答案