試試這個;
private const int PostsPerFeed = 25; //Change this to whatever number you want
那麼你的行動:
public ActionResult Rss()
{
IEnumerable<SyndicationItem> posts =
(from post in model.Posts
where post.PostDate < DateTime.Now
orderby post.PostDate descending
select post).Take(PostsPerFeed).ToList().Select(x => GetSyndicationItem(x));
SyndicationFeed feed = new SyndicationFeed("John Doh", "John Doh", new Uri("http://localhost"), posts);
Rss20FeedFormatter formattedFeed = new Rss20FeedFormatter(feed);
return new FeedResult(formattedFeed);
}
private SyndicationItem GetSyndicationItem(Post post)
{
return new SyndicationItem(post.Title, post.Body, new Uri("http://localhost/posts/details/" + post.PostId));
}
在你FeedResult.cs
class FeedResult : ActionResult
{
private SyndicationFeedFormatter formattedFeed;
public FeedResult(SyndicationFeedFormatter formattedFeed)
{
this.formattedFeed = formattedFeed;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "application/rss+xml";
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
formattedFeed.WriteTo(writer);
}
}
}
筆畫演示是HERE。儘管警告,谷歌Chrome瀏覽器沒有格式
似乎沒有其他方式..所以標記答案是正確的。 – Nirav 2013-03-12 13:57:00