繁体   English   中英

Swashbuckle 错误 - 在 IIS 中运行时出现“swagger.json”的 404

[英]Swashbuckle Error - 404 for 'swagger.json' when running in IIS

我有一个基本的 asp.net 核心 5.0 web API。我安装了 Swashbuckle.ASPNetCore swagger-5.6.3 nuget package。

这是我的 startup.cs。

启动.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 not found。

这有效:

http://localhost/myapp/swagger/v1/swagger.json

但,

http://localhost/myapp/swagger/index.html

给出错误:

错误隐藏获取错误未找到/swagger/v1/swagger.json

我该如何解决这个问题?

关于你的情况,你可以这样做来为 IIS 配置你的Swagger

options.SwaggerEndpoint($"../swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM