ASP.NET MVC RSS Feed Action Result

1 自定义ActionResult

 

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

 

2 在 Action中使用

 

public ActionResult Feed()
{
    SyndicationFeed feed =
        new SyndicationFeed("Test Feed",
                            "This is a test feed",
                            new Uri("http://Contoso/testfeed"), "TestFeedID",
                            DateTime.Now);

    SyndicationItem item =
        new SyndicationItem("Test Item",
                            "This is the content for Test Item",
                            new Uri("http://Contoso/ItemOne"),
                            "TestItemID",
                            DateTime.Now);

    List<SyndicationItem> items = new List<SyndicationItem>();
    items.Add(item);
    feed.Items = items;

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

posted @ 2010-08-09 17:47  小R  阅读(303)  评论(0)    收藏  举报