0

我实现了一个 API 并将其与Swagger集成。

启动配置服务

services
    .AddMvcCore()
    .AddApiExplorer();

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

启动配置

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

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

控制器

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

到目前为止,一切都很好...

奇迹般有效。但是我升级到最新版本[Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer][2]并没有

我遵循了样本文档,但我无法显示我的操作......我在这里错过了什么?

启动配置服务

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

启动配置

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());
     }
})

控制器

//[Route("v1/[controller]")]
[ApiController]
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("1.0")]
public class SomeController : ControllerBase {}
4

1 回答 1

1

这可能是因为您已注释掉.AddApiExplorer()并且您没有调用.AddMvc(). 调用.AddMvcCore()不调用也不.AddApiExplorer()调用(将在 v3.0中.AddVersionedApiExplorer()更改以避免此类问题)。.AddMvc()示例中所示的调用确实.AddApiExplorer()间接调用。这可能是原因,您只需要.AddApiExplorer()重新加入即可。

此外,您正在配置是否有特殊原因:

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

不确定这是否会影响它,但也没有必要。

你有没有在里面设置断点:

services.AddSwaggerGen(c => { … })

app.UseSwaggerUI(c => { … })

AddSwaggerGen中,您应该会看到至少一个 API 描述。内UseSwaggerUI,你至少应该看到1.0。目前尚不清楚您有多少个版本。

于 2018-07-10T17:15:04.010 回答