An implementation of a TrackBack listener in ASP.NET MVC.

Please note: this article was written for the original MVC CTP1 preview in 2007 and is horrifically out of date.

Trackbacks are a form of linkback a way of notify a site that your site has made a reference to it. Trackbacks are slowly being deprecated in favor of pingbacks. The spec can be found at Six Apart.

To provide trackback support we need to do 3 things:

  • Create a trackback Controller and Action
  • Set up an appropriate Route
  • Provide a trackback URL

Lets start by creating our Controller

The ProcessRequest action (part of the LinkBack Controller), processes HTTP POSTs and saves the trackback data to the LinkBack model (in this case a linkback class, the implementation of which is not relevant here). The action first creates a new model, extracts the routing data then extracts the trackback parameters from the HTTP request before saving them to back to the model. If the action is successful a success response is sent otherwise an error. the title, blog_name, title and excerpt are all optional parameters however the url parameter is required.

Successful trackback requests must return the following XML in their response:

Errors are generated as follows:

An unsuccessful request must return the following XML:

Note that in writeXmlToStream() we ensure that the Byte Order Mark is disabled to ensure that it does not cause a parsing error.

Having created an appropriate Controller and Action we now need to hook them up to be routed correctly. Inside the Global.asax file, inside RegisterRoutes() we need to add the following:

The first parameter is the name, the second is the pattern to match, and the third the anonymous type that contains the route data. See MVC URL Routing for more details. Note that trackBackController,trackBackAction and trackBackID are used inside the ProcessRequest() Action.

Our final task is to add a link to the trackback in our web page. This is a simple matter of generating an appropriate link, this page for example has a track back of MVC TrackBack Implementation. The specification states that auto discover can be used, basically a block of XML is embedded in the page so that the trackback URL can be found programmaticly. We generate the XML:

Which produces the following:

Note that the XML is enclosed in quotes to help HTML validators out. So what is the work flow for this system? Firstly a user on a website creates a new post linking to a page on your site. Their site either queries your page and extracts the trackback URL, or is provided the track back URL by the user. Their site sends and HTTP POST to your site, MVC receives the request and routes it to the ProcessRequest action in the LinkBack controller. ProcessRequest extracts the trackback data and saves it to the model.

Finally test trackbacks can be sent using either Simpletracks or Trackback Test Form at the RSS Blog(Internet Explorer only) to confirm your system is operating correctly.