Swashbuckle 错误 - 在 IIS 中 运行 时 'swagger.json' 的 404
Swashbuckle Error - 404 for 'swagger.json' when running in IIS
我有一个基本的 asp.net 核心 5.0 网络 API。我安装了 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 未找到。
这个有效:
http://localhost/myapp/swagger/v1/swagger.json
但是,
http://localhost/myapp/swagger/index.html
给出错误:
Errors Hide Fetch errorNot Found /swagger/v1/swagger.json
我该如何解决这个问题?
关于您的情况,您可以这样做来为 IIS 配置 Swagger
:
options.SwaggerEndpoint($"../swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
我有一个基本的 asp.net 核心 5.0 网络 API。我安装了 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 未找到。
这个有效:
http://localhost/myapp/swagger/v1/swagger.json
但是,
http://localhost/myapp/swagger/index.html
给出错误:
Errors Hide Fetch errorNot Found /swagger/v1/swagger.json
我该如何解决这个问题?
关于您的情况,您可以这样做来为 IIS 配置 Swagger
:
options.SwaggerEndpoint($"../swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());