title: ASP.net Web Api Versioning
body: <p>I have ASP.net Web Api project and I decided that it was time to support versioning. I am using official Microsoft Nuget to support versioning (more info <a href="https://github.com/Microsoft/aspnet-api-versioning" rel="nofollow noreferrer">here</a>), and I decided to version by namespace (as exampled <a href="https://github.com/Microsoft/aspnet-api-versioning/tree/master/samples/webapi/ByNamespaceWebApiSample" rel="nofollow noreferrer">here</a>).</p>
  
  <p>Unfortunately I cannot get code to work. If I call my method like this:</p>
  
  <pre><code>http://localhost:7291/api/Saved/GetNumberOfSavedWorkoutsForUser?api-version=2.0
  </code></pre>
  
  <p>I get error: </p>
  
  <pre><code>Multiple types were found that match the controller named 'Saved'. This can happen if the route that services this request ('api/{controller}/{action}/{id}') found multiple controllers defined with the same name but differing namespaces, which is not supported.
  </code></pre>
  
  <p>And if I call it like this: <code>http://localhost:7291/v2/Saved/GetNumberOfSavedWorkoutsForUser</code></p>
  
  <p>I get error 404:</p>
  
  <pre><code>The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
  </code></pre>
  
  <p>I am not sure what I am doing wrong. Here is my code:</p>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>public void Configuration(IAppBuilder app)
      {
          var configuration = new HttpConfiguration();
          var httpServer = new HttpServer(configuration);
  
  
          app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
  
          // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
          configuration.AddApiVersioning(o =&gt; 
          {
              o.AssumeDefaultVersionWhenUnspecified = true;
              o.ReportApiVersions = true; 
              o.DefaultApiVersion = ApiVersion.Default;
          });
  
  
          configuration.Routes.MapHttpRoute(
              "VersionedUrl",
              "v{apiVersion}/{controller}/{action}/{id}",
              defaults: null,
              constraints: new { apiVersion = new ApiVersionRouteConstraint() });
  
          configuration.Routes.MapHttpRoute(
              "VersionedQueryString",
              "api/{controller}/{action}/{id}",
              defaults: null);
  
  
          app.UseWebApi(httpServer);
  
          ConfigureAuth(app);
      }
  </code></pre>
  
  <p><strong>Saved Controller (v1)</strong></p>
  
  <pre><code>namespace Master.Infrastructure.Api.Controllers
  {
  
      [Authorize]
      [RoutePrefix("api/Saved")]
      [ApiVersion("1.0")]
      public class SavedController : ApiController
      {
  
          private readonly IUserService _userService;
  
          public SavedController(IUserService userService)
          {
              _userService = userService;
  
          }
  
  
          [HttpGet]
          [ActionName("GetNumberOfSavedWorkouts")]
          public async Task&lt;NumberOfSavedWorkouts&gt; GetNumberOfSavedWorkouts()
          {
              var numOfSavedWorkouts = new NumberOfSavedWorkouts
              {
                  CurrentNumberOfSavedWorkouts =
                      await _userService.GetNumberOfSavedWorkoutsForUserAsync(User.Identity.GetUserId())
              };
  
              return numOfSavedWorkouts;
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Saved Controller (v2)</strong></p>
  
  <pre><code>namespace Master.Infrastructure.Api.V2.Controllers
  {
      [Authorize]
      [ApiVersion("2.0")]
      [RoutePrefix("v{version:apiVersion}/Saved")]
      public class SavedController : ApiController
      {
  
          private readonly ISavedWorkoutService _savedWorkoutService;
  
  
          public SavedController(ISavedWorkoutService savedWorkoutService)
          {       
              _savedWorkoutService = savedWorkoutService;
          }
  
  
          [ActionName("GetNumberOfSavedWorkoutsForUser")]
          public async Task&lt;IHttpActionResult&gt; GetNumberOfSavedWorkoutsForUser()
          {
              var cnt = await _savedWorkoutService.CountNumberOfSavedWorkoutsForUser(User.Identity.GetUserId());
  
              return Ok(cnt);
          }
      }
  }
  </code></pre>