Behind the scenes at xbox.com – RSS enabling web marketplace

A number of people were requesting additional RSS feeds for the xbox.com web marketplace. (We had just one that included all new arrivals)

Looking across our site as the various lists of products we display today the significant views are:

  • Browse games by department
  • Search results
  • Promotions (e.g. Deal of the week)
  • Game detail (shows downloads available beneath it)
  • Avatar item browse

These views also have sorting options and a set of filters available for things like product type, game genre, content rating etc.

So we had a couple of options:

  1. Write controller actions that expose the results of specific queries as RSS
  2. Introduce a mechanism whereby any of our product result pages can render as RSS including any user-defined filtering

Our web marketplace is written in ASP.NET MVC (like most of xbox.com) so while option 1 sounds simpler MVC really helps us make option 2 more attractive by way of a useful feature called ActionFilters that let us jump in and reshape the way existing actions behave.

ActionFilters

ActionFilters can be applied to either to an individual action method on a controller or to the controller class itself which applies it to all the actions on that controller. They provide hooks into the processing pipeline where you can jump in and perform additional processing.

The most interesting events are:

  • OnActionExecuting
  • OnActionExecuted
  • OnResultExecuting
  • OnResultExecuted

We’re going to hook in to the OnActionExecuted step – this is because we always want to run after the code in the controller action has executed but before the ActionResult has done it’s work – i.e. before page or RSS rendering.

Writing our ActionFilter

The first thing we want to do is identify that a request wants the RSS version. One way is to read the accepts header and switch when it requests mime/type but this can be a little trickier to test,  another is to append a query parameter on the url which is very easy to test.

Once we’ve identified the incoming request should be for RSS we need to identify the data we want to turn into RSS and re-purpose it.

All the views we identified at the start of this post share a common rendering mechanism and each view model sub-classes from one of our base models. For simplicity though we’ll imagine an interface that just exposes an IEnumerable property.

public class RssEnabledAttribute : ActionFilterAttribute {
  public override void OnActionExecuted(ActionExecutedContext filterContext) {
    var viewModel = filterContext.Controller.ViewData.Model as IProductResultViewModel;
    if (viewModel == null)
        return;

    var rssFeedTitle = FeedHelper.MakeTitle(viewModel.Results);
    filterContext.Controller.ViewData.Add("RssFeedTitle", rssFeedTitle);

    var format = filterContext.RequestContext.HttpContext.Request.QueryString["format"];
    if (format == "rss" && rssFeedTitle != null) {
      var urlHelper = new UrlHelper(filterContext.RequestContext);
      var url = QueryStringUtility.RemoveQueryStringParameter(filterContext.RequestContext.HttpContext.Request.Url.ToString(), "format");
      var feedItems = FeedHelper.GetSyndicationItems(viewModel.Results, urlHelper);
      filterContext.Result = FeedHelper.CreateProductFeed(rssFeedTitle, viewModel.Description, new Uri(url), feedItems);
    }

    base.OnActionExecuted(filterContext);
  }
}

This class relies on our FeedHelper class to achieve three things it needs:

  1. MakeTitle takes the request details – i.e. which page, type of products, filtering and sorting is selected and makes a title by re-using our breadcrumbs
  2. GetSyndicationItems takes the IEnumerable and turns it into IEnumerable by way of a foreach projecting Product into SyndicationItem with some basic HTML formatting, combining the product image and setting the correct category (with a yield thrown in for good measure)
  3. CreateProductFeed then creates a Syndication feed with the appropriate Copyright and Language set and chooses the formatter – in our case RSS 2.0 but could easily be Atom 1.0, e.g.
public static SyndicationFeedResult CreateProductFeed(string title, string description, Uri link, IEnumerable<SyndicationItem> syndicationItems)
{
    var feed = new SyndicationFeed(title, description, link, syndicationItems) {
        Copyright = new TextSyndicationContent(String.Format(Resources.FeedCopyrightFormat, DateTime.Now.Year)),
        Language = CultureInfo.CurrentUICulture.Name
    };

    return new FeedResult(new Rss20FeedFormatter(feed, false));
}

The FeedResult class is a simple one that takes the built-in .NET SyndicationFeed class and wires it up to MVC by implementing an ActionResult that writes the XML of the SyndicationFeedFormatter into the response as well as setting the application/rss+xml content type and encoding.

Advertising the feed in the head

Now that we have the ability to serve up RSS we need to let browsers know it exists.

The ActionFilter we wrote above needs to know the title of the RSS feed regardless of whether it is rendering the RSS (which needs a title) or rendering the page (which will need to advertise the RSS title) so it always calculates it and then puts it into the ViewData dictionary with the key RssFeedTitle.

Now finally our site’s master page can check for the existence of that key/value pair and advertise it out with a simple link tag:

var rssFeedTitle = ViewData["RssFeedTitle"] as string;
if (!String.IsNullOrEmpty(rssFeedTitle)) { %>
<link rel="alternate" type="application/rss+xml" title="<%:rssFeedTitle%>" href="<%:Url.ForThisAsRssFeed%>" />
<% }

This code requires just one more thing – a very small UrlHelper which will append “format=rss” to the query string (taking into account whether there existing query parameters or not).

The result of this is we can now just add [RssEnabled] in front of any controller or action to turn on RSS feeds for that portion of our marketplace! :)

[)amien

7 responses

  1. Avatar for Suzie

    But where ARE the new feeds? You wrote "We had just one that included all new arrivals" but now there seem to be NONE, at least I can't find them... Shouldn't the old URL - https://marketplace.xbox.com/en-US/games/feeds/newreleases.xml - redirect to a page detailing the new locations? I have had no feed for one month, and I'm not going to visit the marketplace each day to check what's new, so without a feed I just stopped buying content... Not ideal!

    Suzie 30 July 2011
  2. Avatar for Methew

    I find it strange that you are talking about .NET all the time and you have made your website in PHP ?

    Just a funny thought, not to offend you.. This indeed a great blog to read :-)

    Kudos

    Methew 5 August 2011
  3. Avatar for Nathanael

    So, here's what I found. You can "generate" an RSS feed by visiting the Marketplace page https://marketplace.xbox.com/en-US/Games/ then select the "Games" type along the top left, choose "SORT BY" at top right and choose "Items Per Page" (if you would like more than 30). Then you can subscribe to the RSS feed for the resulting page. For example, if you choose - Games: Arcade, SORT BY: Release date, Items per Page: 90 this is the resulting feed: https://marketplace.xbox.com/en-US/Games/XboxArcadeGames?pagesize=90&sortby=ReleaseDate&format=rss

    Nathanael 23 August 2011
  4. Avatar for Damien Guard

    Anywhere a list of products is displayed on marketplace you should be able to subscribe - just hit the RSS icon in your browser or put &format=rss on the end of the URL.

    @methew: I think .NET is a much better programming system than PHP. It's more elegant, flexible and can be used for non-web apps. Wordpress is in my opinion the best blogging platform out there. It just happens to be written in PHP.

    It works for browse pages, product detail pages, the promotions (e.g. deal of the week) and search results with any filters applied :)

    Damien Guard 23 August 2011
  5. Avatar for Bob

    Hey Damien. I certainly agree with your preference of using .NET in the web space. I only wish I could ever get a host that supported it without gouging on the price.

    On a somewhat unrelated note... I'm not sure if you hear this question a lot but I've been using your Envy Code R font for well over a year now and I love it dearly. I was wondering if you are still considering updates to it, or is it more or less considered finished?

    Bob 26 August 2011
  6. Avatar for Damien Guard

    Yeah, the hosting can be a problem with ASP.NET.

    Envy Code R definitely isn't finished - the problem is I got very far on the next version but the tool I was using ($499 FontLab Studio 5) managed to trash the widths on hundreds of bitmaps I'd prepared. I also haven't started on the bold and italic sets and have lost track of what changed in terms of glyph shapes etc.

    Damien Guard 26 August 2011
  7. Avatar for Bob

    Ouch! Sounds like you've got your work cut out for you. Well I certainly can't wait for the next version on Envy Code, how ever far down the road that will be. Thanks a lot for creating it. :)

    Bob 26 August 2011