title: RouteHandler for HttpConfiguration
body: <p>I stumbled onto an interesting article about RouteHandlers for ASP.Net MVC's WebAPI:</p>
  
  <p><a href="http://techbrij.com/separate-web-api-action-mobile-asp-net-mvc" rel="nofollow">http://techbrij.com/separate-web-api-action-mobile-asp-net-mvc</a></p>
  
  <p>In this article, it shows how the same WebAPI request from a desktop web browser and from a mobile device are routed to different controllers.  The example uses a RouteHandler like so:</p>
  
  <pre><code>routes.MapHttpRoute(
                 name: "DefaultApi",
                 routeTemplate: "api/{controller}/{id}",
                 defaults: new { id = RouteParameter.Optional }
             ).RouteHandler = new MyRouteHandler();
  </code></pre>
  
  <p>This code is used in Global.asax.</p>
  
  <p>However, I'm registering my WebAPI routes via WebApiConfig in App_Start where HttpConfiguration is passed into the config method:</p>
  
  <pre><code>public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional },
              constraints: null,
              handler: new CustomHttpControllerDispatcher(config)
          );
  
      }
  }
  </code></pre>
  
  <p>config.Routes.MapHttpRoute does not have ability to handle a RouteHandler.  How can I take use a RouteHandler with config.Routes.MapHttpRoute?</p>
  
  <p>Thanks for your help.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After further research, I think I found what I'm looking for, but not sure if this approach is good or not.</p>
  
  <p>My objective is to intercept the incoming request and determine the device type of the request so I can tailor the response to the correct device (desktop, tablet, mobile, etc.).  HttpRequestMessage grants you access to Headers and UserAgent string.  I found a method for analyzing UserAgent to determine the type of device (<a href="http://techbrij.com/display-mode-mobile-tablet-tv-aspnet-mvc" rel="nofollow">http://techbrij.com/display-mode-mobile-tablet-tv-aspnet-mvc</a>).  I'm using a naming convention where I put the DeviceType after the controller name:</p>
  
  <p>Values[DeviceType]Controller</p>
  
  <p>Examples:</p>
  
  <ul>
  <li>ValuesController </li>
  <li>ValuesMobileController </li>
  <li>ValuesTabletController</li>
  </ul>
  
  <p>I determine the devicetype from the UserAgent, then swap out the Controller name in RouteData collection.</p>
  
  <p>**note: DisplayModeConfig.GetDeviceType(userAgent) below uses the approach outlined in the link above</p>
  
  <pre><code>public class CustomHttpControllerDispatcher : HttpControllerDispatcher
  {
      public CustomHttpControllerDispatcher(HttpConfiguration configuration): base(configuration)
      {
  
      }
      protected override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
          // My stuff here
          var userAgent = request.Headers.UserAgent.ToString();
          var deviceType = DisplayModeConfig.GetDeviceType(userAgent);
          IHttpRouteData routeData = request.GetRouteData();
  
          switch(deviceType)
          {
              case "mobile":
                  {
                      var controller = routeData.Values["controller"].ToString();
                      routeData.Values["controller"] = controller + "Mobile";
  
                      break;
                  }
              case "tablet":
                  {
                      var controller = routeData.Values["controller"].ToString();
                      routeData.Values["controller"] = controller + "Tablet";
  
                      break;
                  }
              case "desktop":
                  {
  
                      break;
                  }
          }
  
          return base.SendAsync(request, cancellationToken);
      }
  }
  </code></pre>
  
  <p>By directing which controller handles the response by device type, I can have better control over the user experience by tailoring the correct response to the device type.</p>
  
  <p>What are your thoughts on this approach?  Can you see any problems with this approach?</p>