C# 招摇过市:未能加载API定义
我有一个版本化的API,当我在swagger上选择V1时,它工作正常,当我更改为V2选项时,我收到以下消息: 这是我的Startup.cs 配置服务:C# 招摇过市:未能加载API定义,c#,.net-core,swagger,swagger-ui,C#,.net Core,Swagger,Swagger Ui,我有一个版本化的API,当我在swagger上选择V1时,它工作正常,当我更改为V2选项时,我收到以下消息: 这是我的Startup.cs 配置服务: services.AddApiVersioning( options => { // reporting api versions will return the headers "api-supported-versions" and "api-deprec
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;
} );
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
});
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerApiVersioning>();
配置:
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;
} );
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");
}
}
);
以下是源代码:对于这个特殊的案例,我解决了如下问题: 在配置中:
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;
} );
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");
}
}
);
在配置服务中:
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;
} );
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
});
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerApiVersioning>();
services.AddVersionedApiExplorer(选项=>
{
options.GroupNameFormat=“'v'VVV”;
});
services.AddTransient();
配置服务,如:
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));
}
}
}
公共类配置SwaggerAPVersioning:IConfigureOptions
{
专用只读IApiVersionDescriptionProvider\u提供程序;
公共配置SwaggerAPVersioning(IApiVersionDescriptionProvider提供程序)
{
_提供者=提供者;
}
私有静态信息CreateInfoForApiVersion(ApiVersionDescription描述)
{
返回新信息()
{
//Title=“…”,
Version=description.apivision.ToString(),
//Description=“…”,
联系人=新联系人(){Name=“…”,Email=“…”},
//TermsOfService=“…”
//许可证=新许可证(){Name=“…”,Url=“…”}
};
}
公共void配置(招摇过市选项)
{
foreach(在_provider.ApiVersionDescriptions中的变量描述)
{
options.SwaggerDoc(description.GroupName,CreateInfoForApiVersion(description));
}
}
}