title: ASP.NET Web Api Routing (IIS vs Self Hosted)
body: <p>I have found a minor difference in the routing base classes in ASP.NET Web Api which has forced me to write a little helper class which will allow me to define my routes just once. Is there a reason for this? I'm assuming it was too big a change to the framework to make both <code>RouteCollection</code>s derive from the same base class or implement the same interface (which would have made this class much simpler)</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static class RouteMapper
  {
      private class Route
      {
          public string Name { get; set; }
          public string Template { get; set; }
          public object Defaults { get; set; }
  
          public Route(string name, string template, object defaults)
          {
              Name = name;
              Template = template;
              Defaults = defaults;
          }
      }
  
      private static List&lt;Route&gt; GetRoutes()
      {
          return new List&lt;Route&gt;
                     {
                         new Route(
                             "API Default",
                             "api/{controller}/{id}",
                             new {id = RouteParameter.Optional})
                     };
      }
  
      public static void AddHttpRoutes(this HttpRouteCollection routeCollection)
      {
          var routes = GetRoutes();
          routes.ForEach(route =&gt; routeCollection.MapHttpRoute(route.Name, route.Template, route.Defaults));
      }
  
      public static void AddHttpRoutes(this RouteCollection routeCollection)
      {
          var routes = GetRoutes();
          routes.ForEach(route =&gt; routeCollection.MapHttpRoute(route.Name, route.Template, route.Defaults));            
      }
  }
  </code></pre>
  
  <p>What this allows me to do is to call a simple <code>AddHttpRoutes</code> method in both my Global.asax and my integration tests.</p>
  
  <p><strong>Integration Tests</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>        var configuration = new HttpSelfHostConfiguration("http://localhost:20000");
          configuration.Routes.AddHttpRoutes();
  
          _server = new HttpSelfHostServer(configuration);
          _server.OpenAsync().Wait();
  </code></pre>
  
  <p><strong>Global.asax</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>    public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.AddHttpRoutes();
  
          routes.MapRoute(
              "Default",
              "{controller}/{action}/{id}",
              new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
      }
  </code></pre>
  
  <p>Is this a known issue and is it likely to be fixed in a later release of ASP.NET Web Api?</p>