我有一个版本控制的API,当我大摇摇晃地选择V1时,它工作正常,当我更改为V2选项时,我得到以下消息:
这是我的Startup.cs
ConfigureServices:
services.AddApiVersioning(
options =>
{
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
options.ReportApiVersions = true;
} );
services.AddVersionedApiExplorer(
options =>
{
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
// note: the specified format code will format the version as "'v'major[.minor][-status]"
options.GroupNameFormat = "'v'VVV";
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
// can also be used to control the format of the API version in route templates
options.SubstituteApiVersionInUrl = true;
} );
配置:
app.UseSwagger();
app.UseSwaggerUI(
options =>
{
// build a swagger endpoint for each discovered API version
foreach ( var description in provider.ApiVersionDescriptions )
{
options.SwaggerEndpoint( $"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant() );
}
options.RoutePrefix = string.Empty;
} );
以下是源代码:https://github.com/felipexmitz/api-dotnet-core-basics
答案 0 :(得分:1)
对于这种特殊情况,我已经解决了:
在“配置”中:
app.UseSwagger(options =>
{
options.RouteTemplate = "api/docs/{documentName}/swagger.json";
});
app.UseSwaggerUI
(
options =>
{
options.DocumentTitle = "...";
options.RoutePrefix = "api/docs";
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"{description.GroupName}/swagger.json", "API " + description.GroupName.ToUpperInvariant() + " Specs");
}
}
);
在ConfigureServices中:
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
});
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerApiVersioning>();
和类似的配置服务:
public class ConfigureSwaggerApiVersioning : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider _provider;
public ConfigureSwaggerApiVersioning(IApiVersionDescriptionProvider provider)
{
_provider = provider;
}
private static Info CreateInfoForApiVersion(ApiVersionDescription description)
{
return new Info()
{
//Title = "...",
Version = description.ApiVersion.ToString(),
//Description = "...",
Contact = new Contact() { Name = "...", Email = "..." },
//TermsOfService = "..."
//License = new License() { Name = "...", Url = "..." }
};
}
public void Configure(SwaggerGenOptions options)
{
foreach (var description in _provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
}
}
}