I recently found out that ASP.NET WebPages are the greatest thing ever for small web experiments/projects. You can think of the WebPages framework as MVC but without the M and the C, pretty much just Views. The power in WebPages comes from Razor. If you can write it using Razor, you can stick it in a webpage. In the process of converting one of my hobby projects from MVC to WebPages I forgot about the special RssResult (based of a FileResult) I created for the controller that returns an RSS feed instead of a regular view. Since there is no controller in the WebPages framework, adjustments had to be made. Here is the code that generates an RSS feed in Razor for the WebPages framework:

@{
    Layout = null;

    var ContentType = "application/rss+xml";
    var sourceUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

    // This is one of the things you have to do yourself ;-)
    var items = SomeSite.Net.Code.Blog.GetPosts(5);

    var feedItems = items.Select(p => new System.ServiceModel.Syndication.SyndicationItem(p.Title, p.Body, new Uri(string.Format("{0}/post/{1}", sourceUrl, p.Slug)))
    {
        PublishDate = new DateTimeOffset(p.CreateDate)
    });

    var feed = new System.ServiceModel.Syndication.SyndicationFeed(feedItems)
    {
        Title = new System.ServiceModel.Syndication.TextSyndicationContent("SomeSite.Net", System.ServiceModel.Syndication.TextSyndicationContentKind.Html),
        Description = new System.ServiceModel.Syndication.TextSyndicationContent("A description of SomeSite.Net")
    };

    using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(this.Response.OutputStream))
    {
        feed.GetRss20Formatter().WriteTo(writer);
    }

    this.Response.ContentType = ContentType;
}

There is some custom stuff in there, like the static Blog thing that loads blog posts and a model that contains the actual post. Other than that this the code is all .NET Framework. Or System.ServiceModel to be more specific. As you may already know, the .NET Framework is equipped with a full fledged RSS feed generator. Its hidden in the System.ServiceModel.Syndication namespace.

We start with loading the data we want to display in the RSS feed (in this case, blogposts – their implementation is irrelevant here). Next we create SyndicationItems from the posts we loaded and put those SyndicationItems in a SyndicationFeed. An XmlWriter object is used to hold the output of the SyndicationFeeds Rss20Formatter output. Once we have the RSS feeds data in a stream we simply overwrite the OutputStream of the Response object. Overwriting the response clears everything previously in the response and the last step is to tell the client that we are serving application/rss+xml as content instead of html. Requesting the page now results in an RSS feed instead of a normal page!

Sander Harrewijnen

Author Sander Harrewijnen

Als ontwikkelaar mag ik graag problemen oplossen en nieuwe functionaliteit bouwen. Vanuit mijn creatieve kant hou ik me ook graag bezig met hoe iets bij de eindgebruiker aankomt. Je kunt bij mij dus ook terecht voor de opmaak en layout van een oplossing. Als je mij wilt afleiden, dan lukt dat altijd met een coole nieuwe gadget. Dat is namelijk mijn andere passie.

More posts by Sander Harrewijnen
24 April 2014

Leave a Reply