Tuesday 2 July 2013

C# ASP.NET MVC4 - How to Generate an Atom or RSS Feed

.NET's syndication classes make creating RSS/Atom feeds easy. Here's how to do it in ASP.NET MVC. We're using Atom below, but switching to RSS is simply a matter of changing the Atom10FeedFormatter to Rss20FeedFormatter.

The feed url in this example would be http://myserver/myappname/feed

 public class FeedResult : ActionResult  
   {  
     public Encoding ContentEncoding { get; set; }  
     public string ContentType { get; set; }  
     private readonly SyndicationFeedFormatter _feed;  
     public SyndicationFeedFormatter Feed  
     {  
       get { return _feed; }  
     }  
     public FeedResult(SyndicationFeedFormatter feed)  
     {  
       _feed = feed;  
     }  
     public override void ExecuteResult(ControllerContext context)  
     {  
       if (context == null)  
         throw new ArgumentNullException("context");  
       HttpResponseBase response = context.HttpContext.Response;  
       response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/rss+xml";  
       if (ContentEncoding != null)  
         response.ContentEncoding = ContentEncoding;  
       if (_feed != null)  
       {  
         using (var xmlWriter = new XmlTextWriter(response.Output))  
         {  
           xmlWriter.Formatting = Formatting.Indented;  
           _feed.WriteTo(xmlWriter);  
         }  
       }  
     }  
   }   
 

 
   public class FeedController : Controller  
   {  
     private readonly IPostingDataSource _db;  
     private static readonly Settings Settings = new Settings();  
     private const int ItemContentLength = 500; 
 
     public FeedController()  
     {  
       _db = new PostingDbContext();  
     }  
     public ActionResult Index()  
     {  
       string appUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;  
       string detailsUrl = appUrl + "/Home/Details/";  
       var postItems = _db.Postings  
           .OrderByDescending(x => x.AddedDate)  
           .Take(20)  
           .AsEnumerable()  
           .Select(p => new SyndicationItem(p.Title, TruncateLongString(p.Body, ItemContentLength, true), new Uri(detailsUrl + p.Id), p.Id.ToString(), new DateTimeOffset(p.AddedDate)));  
       var feed = new SyndicationFeed("bCay Auctions", "Buy! Sell!", new Uri(appUrl), postItems)  
       {  
         Copyright = new TextSyndicationContent("Codestruck Productions"),  
         Language = "en-US"  
       };  
       feed.Authors.Add(new SyndicationPerson(Settings.Email, "Codestruck", appUrl));  
       // Can change from Atom to RSS here  
       return new FeedResult(new Atom10FeedFormatter(feed));  
     }  
     private static string TruncateLongString(string str, int maxLength, bool addEllipsis)  
     {  
       if (str.Length <= maxLength)  
         return str;  
       string result = str.Substring(0, Math.Min(str.Length, maxLength));  
       if (addEllipsis)  
         result += "...";  
       return result;  
     }  
   }  

No comments:

Post a Comment