title: WebApi – controllers inheritance – new versions of methods
body: <p>I use Web API 2. I provide clients methods in version1:</p>
  
  <pre><code>http://localhost/version1/api/base 
  http://localhost/version1/api/values
  </code></pre>
  
  <p>It is mine controller:</p>
  
  <pre><code>[RoutePrefix("version1")]
  public class ValuesController : ApiController
  {
      [Route("api/base")]
      public string GetBaseMethod()
      {
          return "bbb";
      }
  
      [Route("api/values")]
      public string GetVersion1()
      {
          return "aaa";
      }   
  }
  </code></pre>
  
  <p>Now I would like to provide clients in version2 methods:</p>
  
  <ul>
  <li>/version2/api/base (method the same like in version1)</li>
  <li>/version2/api/values (method has changed, now it returns int, not string but address must be the same like in version1: /api/values)</li>
  </ul>
  
  <p>So I have the second controller with inheritance:</p>
  
  <pre><code>[RoutePrefix("version2")]
  public class Values2Controller : ValuesController
  {
      [Route("api/values")]
      public int GetVersion2()
      {
          return 5;
      }
  }
  </code></pre>
  
  <p>I have enabled attribute routes inheritance in WebApiConfig.cs:</p>
  
  <pre><code>public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          config.MapHttpAttributeRoutes(new CustomDirectRouteProvider());
  
          config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );          
      }
  }
  
  public class CustomDirectRouteProvider : DefaultDirectRouteProvider
  {
      protected override IReadOnlyList&lt;IDirectRouteFactory&gt; GetActionRouteFactories(HttpActionDescriptor actionDescriptor)
      {
          return actionDescriptor.GetCustomAttributes&lt;IDirectRouteFactory&gt;(inherit: true);
      }
  }
  </code></pre>
  
  <p>Result:</p>
  
  <p><code>http://localhost/version1/api/base</code></p>
  
  <ul>
  <li>it works.</li>
  </ul>
  
  <p><code>http://localhost/version2/api/base</code></p>
  
  <ul>
  <li>it works.</li>
  </ul>
  
  <p><code>http://localhost/version1/api/values</code></p>
  
  <ul>
  <li>it works.</li>
  </ul>
  
  <p><code>http://localhost/version2/api/values</code></p>
  
  <ul>
  <li><strong>it DOESN”t work. I have error</strong>:</li>
  </ul>
  
  <blockquote>
    <p>{
        "Message": "An error has occurred.",
        "ExceptionMessage": "Multiple actions were found that match the request: \r\nGetVersion2 on type
    WebApplication1.Controllers.Values2Controller\r\nGetVersion1 on type
    WebApplication1.Controllers.Values2Controller",
        "ExceptionType": "System.InvalidOperationException",
        "StackTrace": "   w System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext
    controllerContext)\r\n   w
    System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext
    controllerContext)\r\n   w
    System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext
    controllerContext, CancellationToken cancellationToken)\r\n   w
    System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"
    }</p>
  </blockquote>
  
  <p>What can I do?</p>