Cook Computing

XML-RPC.NET and ASP.NET MVC

August 2, 2010 Written by Charles Cook

Scott Hanselman makes some nice comments about XML-RPC.NET in his blog today. His mention of ASP.NET MVC and XmlRpcRoutes caught my attention:

You just need to derive from Charles' XmlRpcService and he'll handle the routing of the HTTP POSTs and the calling of the methods and tearing apart of the parameters. (Actually, as a curiosity back in the ASP.NET MVC 1.0 timeframe both Phil and I write XmlRpcRoutes and supporting samples just to see if it was possible. It is.)

I recently implemented the MetaWeblog API in my home-brew blog engine using an ASP.NET MVC route handler. In the same way as Scott describes I implemented the IMetaWeblog API in a class derived from XmlRpcServer and the required interface:


public class MetaWeblog : XmlRpcService, IMetaWeblog
{
  // ...
  // implementation of IMetaWeblog methods
  // ...
}

I then implemented an ASP.NET MVC route handler:


public class MetaWeblogRouteHandler : IRouteHandler
{
  public IHttpHandler GetHttpHandler(RequestContext requestContext)
  {
    return new MetaWeblog();
  }
}

And finally added a route for the route handler in the routing table:


public class MvcApplication : System.Web.HttpApplication
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.Add(new Route("{weblog}", null, 
      new RouteValueDictionary(new { weblog = "blogapi" }), 
      new MetaWeblogRouteHandler()));

    // ...
  }
 // ...
}