code_junkie

RSS Feeds in ASP.NET MVC

Nov 10th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. public class RssActionResult : ActionResult
  2. {
  3. public SyndicationFeed Feed { get; set; }
  4.  
  5. public override void ExecuteResult(ControllerContext context)
  6. {
  7. context.HttpContext.Response.ContentType = "application/rss+xml";
  8.  
  9. Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
  10. using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
  11. {
  12. rssFormatter.WriteTo(writer);
  13. }
  14. }
  15. }
  16.  
  17. return new RssActionResult() { Feed = myFeedInstance };
  18.  
  19. <%@ Page ContentType="application/rss+xml" Language="C#" AutoEventWireup="true" CodeBehind="PostRSS.aspx.cs" Inherits="rr.web.Views.Blog.PostRSS" %><?xml version="1.0" encoding="utf-8"?>
  20. <rss version="2.0">
  21. <channel>
  22. <title>ricky rosario's blog</title>
  23. <link>http://<%= Request.Url.Host %></link>
  24. <description>Blog RSS feed for rickyrosario.com</description>
  25. <lastBuildDate><%= ViewData.Model.First().DatePublished.Value.ToUniversalTime().ToString("r") %></lastBuildDate>
  26. <language>en-us</language>
  27. <% foreach (Post p in ViewData.Model) { %>
  28. <item>
  29. <title><%= Html.Encode(p.Title) %></title>
  30. <link>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></link>
  31. <guid>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></guid>
  32. <pubDate><%= p.DatePublished.Value.ToUniversalTime().ToString("r") %></pubDate>
  33. <description><%= Html.Encode(p.Content) %></description>
  34. </item>
  35. <% } %>
  36. </channel>
  37. </rss>
Add Comment
Please, Sign In to add comment