0

I implemented an API and integrated it with Swagger.

Startup ConfigureServices:

services
    .AddMvcCore()
    .AddApiExplorer();

services.AddSwaggerGen(c => {
    c.SwaggerDoc("v1", new Info { 
        Title = "API", Version = "v1" 
    });
});

Startup Configure

app.UseSwagger(c => {
    c.RouteTemplate = "{documentName}/configuration.json";
});

app.UseSwaggerUI(c =>
{
     c.SwaggerEndpoint("/v1/configuration.json", "API");
     c.RoutePrefix = "v1/docs";
})

Controller

[Route("v1/[controller]")]
[ApiController]
public class SomeController : ControllerBase {}

So far so good...

Works like a charm. However my upgrade to the latest [Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer][2] doesn't

I followed both the samples as well as the documentation but I can't get my operations to show... What am I missing here?

Startup ConfigureServices

services
    .AddMvcCore()
    //.AddApiExplorer()
    .AddVersionedApiExplorer( o => o.GroupNameFormat = "'v'VVV" );

services.AddApiVersioning(o => o.ReportApiVersions = true);

services.AddSwaggerGen(c => {
//    c.SwaggerDoc("v1", new Info { 
//        Title = "API", Version = "v1" 
//    });

      foreach (var description in provider.ApiVersionDescriptions)
      {
          c.SwaggerDoc(
              description.GroupName,
              new Info()
              {
                  Title = $"API",
                  Version = description.ApiVersion.ToString()
              });
      }
      c.OperationFilter<SwaggerDefaultValues>();
});

*SwaggerDefaultValues.cs

Startup Configure

app.UseSwagger(c => {
    c.RouteTemplate = "{documentName}/configuration.json";
});

app.UseSwaggerUI(c =>
{
     c.SwaggerEndpoint("/v1/configuration.json", "API");
     c.RoutePrefix = "v1/docs";

     foreach (var description in apiProvider.ApiVersionDescriptions)
     {
         c.SwaggerEndpoint($"/{description.GroupName}/configuration.json", description.GroupName.ToUpperInvariant());
     }
})

Controller

//[Route("v1/[controller]")]
[ApiController]
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("1.0")]
public class SomeController : ControllerBase {}
Ropstah
  • 17,538
  • 24
  • 120
  • 194

1 Answers1

1

This is probably because you have commented out .AddApiExplorer() and you do not have a call to .AddMvc(). The call to .AddMvcCore() does not call .AddApiExplorer() nor does .AddVersionedApiExplorer() (which will changing in v3.0 to avoid this kind of issue). The call to .AddMvc() as shown in the examples does call .AddApiExplorer() indirectly. This is likely the cause and you just need to bring .AddApiExplorer() back into the fold.

In addition, is there a particular reason you are configuring:

c.SwaggerEndpoint("/v1/configuration.json", "API");
c.RoutePrefix = "v1/docs";

Not sure if that's affecting it, but shouldn't be necessary either.

Have you set breakpoints inside:

services.AddSwaggerGen(c => { … })

And

app.UseSwaggerUI(c => { … })

Within AddSwaggerGen, you should see at least one API description. Within UseSwaggerUI, you should see at least 1.0. It's not clear how many versions you have at the moment.

Chris Martinez
  • 3,185
  • 12
  • 28