title: web-api & swagger documentation
body: <p>I've written a asp web-api and I've added versioning to the api using <a href="http://aspnet.codeplex.com/SourceControl/changeset/view/dd207952fa86#Samples/WebApi/NamespaceControllerSelector/ReadMe.txt" rel="noreferrer">this class</a>.</p>
  
  <p>My routes look like this:</p>
  
  <pre><code>config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "{namespace}/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
  );
  
  config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));
  </code></pre>
  
  <p>So I can access <code>https://localhost:44301/v1/test</code></p>
  
  <p>I'm now trying to add Swagger to produce some api documentation (actually I'm using the <a href="https://www.nuget.org/packages/Swashbuckle/" rel="noreferrer">Swashbuckle Nuget</a> package)</p>
  
  <p>When I browse to <code>/swagger</code> the page loads but comes up with errors for all the controllers:</p>
  
  <pre><code>Unable to read api 'TestController' from path https://localhost:44301/swagger/api-docs/TestController (server returned undefined)
  </code></pre>
  
  <p>If I browse to <code>https://localhost:44301/swagger/api-docs/TestController</code> it comes up with a <code>NullReferenceException</code> with a stack trace of:</p>
  
  <pre><code>at Swashbuckle.Swagger.OperationGenerator.CreateParameter(ApiParameterDescription apiParamDesc, String apiPath)
     at Swashbuckle.Swagger.OperationGenerator.&lt;&gt;c__DisplayClass2.&lt;ApiDescriptionToOperation&gt;b__1(ApiParameterDescription paramDesc)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at Swashbuckle.Swagger.OperationGenerator.ApiDescriptionToOperation(ApiDescription apiDescription)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.OrderedEnumerable`1.&lt;GetEnumerator&gt;d__0.MoveNext()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at Swashbuckle.Swagger.ApiExplorerAdapter.CreateApi(IGrouping`2 apiDescriptionGroup, OperationGenerator operationGenerator)
     at Swashbuckle.Swagger.ApiExplorerAdapter.&lt;&gt;c__DisplayClassb.&lt;GetDeclaration&gt;b__7(IGrouping`2 apiDescGrp)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.OrderedEnumerable`1.&lt;GetEnumerator&gt;d__0.MoveNext()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at Swashbuckle.Swagger.ApiExplorerAdapter.GetDeclaration(String basePath, String version, String resourceName)
     at Swashbuckle.Application.CachingSwaggerProvider.&lt;&gt;c__DisplayClass4.&lt;GetDeclaration&gt;b__3(String k)
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
     at Swashbuckle.Application.CachingSwaggerProvider.GetDeclaration(String basePath, String version, String resourceName)
     at Swashbuckle.Application.SwaggerSpecHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
     at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
     at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
     at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
     at System.Web.Http.HttpServer.&lt;&gt;n__FabricatedMethod9(HttpRequestMessage , CancellationToken )
     at System.Web.Http.HttpServer.&lt;SendAsync&gt;d__0.MoveNext()
  </code></pre>
  
  <p>If I change the route to this:</p>
  
  <pre><code>config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
  )
  </code></pre>
  
  <p>The Swagger pages then work, but the actual api doesn't!</p>
  
  <p>So the question is, how can I make Swagger work with a versioned api?</p>