Multiple types were found that match the controller
<p>I'm trying to <code>api versioning</code> using <code>header</code> but in <code>different folder structure</code> like below.</p>

<p>In <code>Controller folder</code> have <code>V1</code> sub folder inside that <code>CustomerController.cs</code>
and </p>

<p>In <code>Controller folder</code> have <code>V2</code> sub folder inside that <code>CustomerController.cs</code></p>

<p>when I user api version using <code>URL</code> above works fine.</p>

<p>my issue is When I try this approach with <code>header</code> it is giving me below error:</p>

<blockquote>
  <p>{   "Message": "An error has occurred.",   "ExceptionMessage":
  "Multiple types were found that match the controller named 'customer'.
  This can happen if the route that services this request
  ('api/{controller}/{id}') found multiple controllers defined with the
  same name but differing namespaces, which is not supported.\r\n\r\nThe
  request for 'customer' has found the following matching
  controllers:\r\nApiVersion.Controllers.V1.CustomerController\r\nApiVersion.Controllers.V2.CustomerController",   "ExceptionType": "System.InvalidOperationException",   "StackTrace": "
  at
  System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage
  request)\r\n   at
  System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"
  }</p>
</blockquote>

<p>WebApiConfig.cs</p>

<pre><code> public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
</code></pre>

<p>CustomeControllerSelector.cs</p>

<pre><code>        public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            var controllers = GetControllerMapping();
            var routeData = request.GetRouteData();
            var controllerName = routeData.Values["controller"].ToString();
            //HttpControllerDescriptor controllerDescriptor;

            var version = GetVersionFromAcceptHeaderVersion(request);
            var controllerKey = String.Format(CultureInfo.InvariantCulture, "{0}.{1}",
                        version, controllerName);


            //if (controllers.TryGetValue(controllerName, out controllerDescriptor))
            // {

            if (!string.IsNullOrEmpty(version))
            {
                HttpControllerDescriptor versionedControllerDescriptor;
                if (controllers.TryGetValue(controllerKey, out versionedControllerDescriptor))
                {
                    return versionedControllerDescriptor;
                }
            }
            return null;
        }

          private string GetVersionFromAcceptHeaderVersion(HttpRequestMessage request)
        {
            var acceptHeader = request.Headers.Accept;
            foreach (var mime in acceptHeader)
            {
                if (mime.MediaType == "application/json" || mime.MediaType == "text/html")
                {
                    var version = mime.Parameters
                                    .Where(v =&gt; v.Name.Equals("version", StringComparison.OrdinalIgnoreCase))
                                    .FirstOrDefault();
                    if (version != null)
                    {
                        return version.Value;
                    }
                    return string.Empty;
                }
            }
            return string.Empty;
        }
</code></pre>