Swagger:无法加载API定义

问题描述 投票:1回答:1

我有一个版本化的API,当我在swagger上选择V1它工作正常,当我更改为V2选项时,我收到此消息:

enter image description here

这是我的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

c# .net-core swagger swagger-ui
1个回答
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));
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.