我有一个基本的asp.net核心5.0WebAPI。我安装了Swashbuckle.ASPNetCore swagger-5.6.3 nuget软件包。
这是我的startup.cs。
startup.cs:
public void ConfigureServices(IServiceCollection services)
{
#region Api Versioning
services.AddVersioning();
#endregion
#region Swagger
services.AddSwagger();
#endregion
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider, IApiVersionDescriptionProvider provider, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
{
app.UseMiddleware<ExceptionMiddleware>();
app.UseStatusCodePagesWithReExecute("/errors/{0}");
#region Swagger
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseVersionedSwagger(provider);
#endregion
#region CORS
app.UseCors("AllowAllOrigins");
#endregion
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
以下是详细信息:
public static class ApiVersioningExtension
{
public static IServiceCollection AddVersioning(this IServiceCollection services)
{
services.AddApiVersioning(
options =>
{
options.ReportApiVersions = true;
});
services.AddVersionedApiExplorer(
options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
return services;
}
}
private static string XmlCommentsFilePath
{
get
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var fileName = typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml";
return Path.Combine(basePath, fileName);
}
}
public static IServiceCollection AddSwagger(this IServiceCollection services)
{
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
services.AddSwaggerGen(options =>
{
options.OperationFilter<SwaggerDefaultValues>();
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Scheme = "Bearer",
Description = @"JWT Authorization header using the Bearer scheme. <br><br>
Enter 'Bearer' [space] and then your token in the text input below.
<br><br>Example: 'Bearer 12345abcdef'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http
});
options.OperationFilter<SwaggerAuthorizeCheckOperationFilter>();
// integrate xml comments
options.IncludeXmlComments(XmlCommentsFilePath);
});
return services;
}
public static IApplicationBuilder UseVersionedSwagger(this IApplicationBuilder app, IApiVersionDescriptionProvider provider)
{
app.UseSwagger();
app.UseSwaggerUI(
options =>
{
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
return app;
}
public class SwaggerDefaultValues : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var apiDescription = context.ApiDescription;
operation.Deprecated |= apiDescription.IsDeprecated();
if (operation.Parameters == null)
{
return;
}
foreach (var parameter in operation.Parameters)
{
var description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);
if (parameter.Description == null)
{
parameter.Description = description.ModelMetadata?.Description;
}
if (parameter.Schema.Default == null && description.DefaultValue != null)
{
parameter.Schema.Default = new OpenApiString(description.DefaultValue.ToString());
}
parameter.Required |= description.IsRequired;
}
}
}
当我在IIS Express上运行这个程序时,swagger运行得很好。然后我把这个部署到IIS,我得到404找不到。
这样做是可行的:
但,
给出错误:
错误隐藏Fetch errorNot Found /swagger/v1/swagger.json
我该怎么解决这个问题?
发布于 2022-02-09 12:17:11
对于您的情况,您可以这样做,以便为IIS配置Swagger
:
options.SwaggerEndpoint($"../swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
https://stackoverflow.com/questions/71048989
复制相似问题