导航

swagger 兼容 docker 转发 配置

Posted on 2021-02-14 10:42  DotNet1010  阅读(270)  评论(0编辑  收藏  举报
            app.UseSwagger(c =>
            {
                c.PreSerializeFilters.Add((swagger, httpReq) =>
                {
                    string swagger_index_url = httpReq.Headers["Referer"];
                    var index = swagger_index_url.IndexOf("/swagger/index.html", 0, StringComparison.OrdinalIgnoreCase);
                    var swagger_server_prefix = httpReq.Host.Value;
                    if (index > 0)
                    {
                        swagger_server_prefix = swagger_index_url.Substring(0, index + 1);
                    }
                    swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = swagger_server_prefix } };
                });

            });
            app.UseSwaggerUI(c =>
            {
                //使用相对路径
                c.SwaggerEndpoint("v1/swagger.json", "API");
            });