Tuesday, December 15, 2009
About a month back Rob Conery mentioned that he was going to create a blogging application using MVC, which caused me to think to myself: “SELF! why don’t you try something like that and see what you can come up with?” So in my spare time when I am not doing real work or at home shuttling kids around, I’ve been playing around with this idea. I am really happy with my BlogEngine.Net set up and probably will continue to use it even if I were to finish this app but I figure this would be an opportunity to just do something different than I have been doing lately and learn some new things that I have not had a chance to work with as of yet.
So with that in mind, one of the first things I decided to tackle was going about syndicating an RSS feed and as it turns out it is not too terribly difficult to do; although, there are some things I did struggle with—uhhh---but I am getting ahead of myself.
Build you own ActionResult
I mention in my previous post that ASP.Net come with several different controller actions, and the one that I used in the that example was the JsonResult. Well JsonResult really is just a class that is derived from the abstract class ActionResult. I can write my own class that derives from ActionResult to return back to the view an RSS feed which is what I am going to do in this example.
So in my custom RssResult that inherits from ActionResult, I need to then override the ExecuteResult method. Inside that method I then need to set the response type to “application/rss+xml” so it can be rendered as RSS. The next thing to notice is that in my constructor I am passing in an object of type SyndicationFeed which is a part of WCF. It is in the System.ServiceModel.Syndication namespace. You can find it by referencing the System.ServiceModel.Web assembly. I can then use the Rss20FeedFormatter to generate XML which is RSS compliant.
Building the SyndicationFeed
Building the SyndicationFeed object is where I had the most trouble. If you just want a simple compliant RSS feed, this guy works great; however, as you can see just from looking at my own RSS feed there many other elements you can extend to an RSS feed and adding those to the SyndicationFeed element is a bit tricky.
From the code you can see that when I am instantiating the SyndicationFeed object I am passing in the title of the blog, the blog description, and the URL. Where it got a bit hairy, was adding other elements that prefixed a common namespace. I could add the namespace for each element (as seen in the code) but there does not seem to be a way to put the common namespaces in say the channel element and then just use the prefix to those namespaces later. I ended up having to repeat the same namespaces over again. But that being said, it is still pretty easy to build the feed.
Once the feed object is ready I then could add each of the posts by mapping them to a SyndicationItem object and then add those collection of objects to the items collection in the feed.
The Controller Class
Now that I have my RssResult and my functionality to build the SyndicationFeed object, I can then create an action inside my controller class that puts the two together.
So when I browse to domain/controller/action in this case http://localhost:64533/Syndication/Rss I get the following RSS sample:
Looks pretty good, although, if you look at my actual RSS feed and this one, you will notice that the name spaces are repeated verses being listed at the top as they probably should be. I need to play around with it some more but this gets the job done and I did not have to write all that much code, which is nice.