title: Testing route configuration in ASP.NET WebApi
body: <p>I am trying to do some unit testing of my <a href="http://www.asp.net/web-api" rel="noreferrer">WebApi</a> route configuration. I want to test that the route <code>"/api/super"</code> maps to the <code>Get()</code> method of my <code>SuperController</code>. I've setup the below test and am having a few issues.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void GetTest()
  {
      var url = "~/api/super";
  
      var routeCollection = new HttpRouteCollection();
      routeCollection.MapHttpRoute("DefaultApi", "api/{controller}/");
  
      var httpConfig = new HttpConfiguration(routeCollection);
      var request = new HttpRequestMessage(HttpMethod.Get, url);
  
      // exception when url = "/api/super"
      // can get around w/ setting url = "http://localhost/api/super"
      var routeData = httpConfig.Routes.GetRouteData(request);
      request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
  
      var controllerSelector = new DefaultHttpControllerSelector(httpConfig);
  
      var controlleDescriptor = controllerSelector.SelectController(request);
  
      var controllerContext =
          new HttpControllerContext(httpConfig, routeData, request);
      controllerContext.ControllerDescriptor = controlleDescriptor;
  
      var selector = new ApiControllerActionSelector();
      var actionDescriptor = selector.SelectAction(controllerContext);
  
      Assert.AreEqual(typeof(SuperController),
          controlleDescriptor.ControllerType);
      Assert.IsTrue(actionDescriptor.ActionName == "Get");
  }
  </code></pre>
  
  <p>My first issue is that if I don't specify a fully qualified URL <code>httpConfig.Routes.GetRouteData(request);</code> throws a <code>InvalidOperationException</code> exception with a message of "This operation is not supported for a relative URI."</p>
  
  <p>I'm obviously missing something with my stubbed configuration. I would prefer to use a relative URI as it does not seem reasonable to use a fully qualified URI for route testing.</p>
  
  <p>My second issue with my configuration above is I am not testing my routes as configured in my RouteConfig but am instead using:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var routeCollection = new HttpRouteCollection();
  routeCollection.MapHttpRoute("DefaultApi", "api/{controller}/");
  </code></pre>
  
  <p>How do I make use of the assigned <code>RouteTable.Routes</code> as configured in a typical Global.asax:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MvcApplication : HttpApplication
  {
      protected void Application_Start()
      {
          // other startup stuff
  
          RouteConfig.RegisterRoutes(RouteTable.Routes);
      }
  }
  
  public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
          // route configuration
      }
  }
  </code></pre>
  
  <p>Further what I have stubbed out above may not be the best test configuration. If there is a more streamlined approach I am all ears.</p>