当替换一些请求路径时,如何设置Swashbucle以查找swagger.json?

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

我正在尝试将.net api升级到.net core 3.1。为此,我还必须升级到Swashbuckle.AspNetCore 5.0。这在localhost中是开箱即用的,但是在部署时找不到swagger.json文件。

Startup.cs

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {

        app.Use(async (context, next) =>
        {
            context.Request.Path = context.Request.Path.Value.Replace("/api-svc", "");
            await next.Invoke();
        });
        app.UseSwagger();

        app.UseSwaggerUI(s =>
        {
            s.SwaggerEndpoint("/swagger/v1/swagger.json", "api v1");
        });
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseDefaultFiles();
        app.UseMiddleware<CustomExceptionHandlerMiddleware>();
        app.UseResponseCompression();
        app.UseCors("AllowAll");
        app.UseRouting();
        app.UseAuthentication();
        app.UseMvc();
    }

获取错误:未定义/swagger/v1/swagger.json

网络显示它正在尝试从以下URL检索文件:https://api.com/swagger/v1/swagger.json正确给出错误的地方:

负载均衡器没有匹配的路由。您的请求有问题!。

URL应该是:https://api.com/api-svc/swagger/v1/swagger.json。因为当我手动输入此内容时,它会返回json文件。

所以我试图设置基本路径(https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1173);

        if (env.IsDevelopment())
        {
            app.UseSwagger();

            app.UseSwaggerUI(s =>
            {
                s.SwaggerEndpoint("/swagger/v1/swagger.json", "api v1");
            });
        }
        else
        {
            app.UseSwagger(c =>
            {
                c.PreSerializeFilters.Add((swaggerDoc, request) =>
                {
                    swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{request.Scheme}://{request.Host.Value}{Configuration["VIRTUAL_PATH"]}" } };
                });
            });
            app.UseSwaggerUI(s =>
            {
                s.SwaggerEndpoint("/swagger/v1/swagger.json", "api");
            });
        }

但是相同的错误仍然存​​在。谁能指出我正确的方向?

[更新]

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1253)使用相对路径使一切看起来正确:

   app.UseSwagger();
   app.UseSwaggerUI(s => {
                s.SwaggerEndpoint("v1/swagger.json", "api v1");
            });

现在的问题是,实际的api请求url应该为https://api.com/health时为https://api.com/api-svc/health

asp.net-core swagger swagger-ui swashbuckle
1个回答
0
投票

好吧,经过大量测试,我找到了答案:

  if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI(s =>
                {
                    s.SwaggerEndpoint("v1/swagger.json", "api v1");
                });
            }
            else
            {
                app.UseSwagger(c =>
                {
                    c.PreSerializeFilters.Add((swaggerDoc, request) =>
                    {
                        var path = Configuration["VIRTUAL_PATH"];
                        swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"https://{request.Host.Value}{path}" } };
                    });
                });
                app.UseSwaggerUI(s =>
                {
                    s.SwaggerEndpoint("v1/swagger.json", "api");
                });
            }
© www.soinside.com 2019 - 2024. All rights reserved.